Python发布到远程主机

时间:2016-09-08 11:04:27

标签: python django

您好我想写机器人在toefl测试中注册,我发送请求到远程网站:

from django.http import HttpResponse

import requests

from bs4 import BeautifulSoup
from django.middleware import csrf


def index(request):
    session = requests.Session()
    page = session.get('https://toefl-registration.ets.org/TOEFLWeb/extISERLogonPrompt.do')


    if request.method == 'POST':
        url = 'https://toefl-registration.ets.org/TOEFLWeb/logon.do'
        data = {
                'password': request.POST.get('password', ''),
                'username': request.POST.get('username', ''),
                'org.apache.struts.taglib.html.TOKEN': request.POST.get('org.apache.struts.taglib.html.TOKEN', '')
               }

        page = session.post(url, data=data)


    return HttpResponse(request,page.text)

我的帖子请求与toefl原始网站发布的帖子请求不同,而不是登录显示错误:我们很抱歉,我们的服务器目前正忙。请等待几分钟,然后重试您的请求。

有什么问题?其他公司的某人没有错误就这样做了

1 个答案:

答案 0 :(得分:0)

您应该确保正确输入Cookie,如:

def index(request):

response = HttpResponse()
if 'JSESSIONID' in request.COOKIES:
    print('1')
else:
    session = requests.Session()
    page = session.get('https://toefl-registration.ets.org/TOEFLWeb/extISERLogonPrompt.do')
    cookie = page.cookies
    for c in cookie:
        response.set_cookie(c.name, c.value)

iud = '1'
if request.method == 'POST':
    url = 'https://toefl-registration.ets.org/TOEFLWeb/logon.do'
    iud = '2'
    page = requests.post(url, data=data, cookies=request.COOKIES)
    cookie = page.cookies



response.write(pageReader(request, page.text, iud))
return response