当尝试使用python登录时,网站返回501

2024-06-16 18:05:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我说的是这个网站:

http://www.belegger.nl/mijnbelegger/voorpagina

我正在尝试按以下方式登录:

def login(self, username, password):
    #form_doc: a lxml.html object
    form_doc = self.browser.getdoc("http://www.belegger.nl/mijnbelegger/voorpagina")
    form_html = form_doc.cssselect("div.loginPanel form")[0]
    form_dict = {inp.get('name') : inp.get('value') for inp in form_html.cssselect("input")}
    form_dict['username'] = username
    form_dict['password'] = password

    #form_dict now contains all the correct inputs and their values

    #then, i precisely copy all the headers of a successful browser login:
    self.add_headers()        

    #what follows is a POST request:
    self.browser.open("http://www.belegger.nl/mijnbelegger/voorpagina", self.browser.urlencode(form_dict))

def add_headers(self):
    headers = {
        'Host' : 'www.belegger.nl',
        'User-Agent' : 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0',
        'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language' : 'en-gb,en;q=0.5',
        'Accept-Encoding' : 'gzip, deflate',
        'Referer' : 'http://www.belegger.nl/mijnbelegger/profiel',
        'Content-Length' : '187',
        'Content-Type' : 'text/plain; charset=UTF-8',
        'Connection' : 'keep-alive',
        'Pragma' : 'no-cache',
        'Cache-Control' : 'no-cache'
    }
    for header in headers.items():
        self.browser.opener.addheaders.append(header)

下面是最终的请求:

POST /mijnbelegger/voorpagina HTTP/1.1
Content-Length: 115
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0
Host: www.belegger.nl
Referer: http://www.belegger.nl/mijnbelegger/profiel
Pragma: no-cache
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded

formtoken=c5851960db739b61bc28afd4f23cec2badacb807&username=xxx&password=xxx&Inloggen=Inloggen

python中的请求与firefox中的请求几乎完全相似,唯一的区别是“connection”头,它似乎不能通过urllib2更改。你知道吗

但还有更多:

如果我尝试使用firefox插件livehttpheaders重新成功登录,我会得到相同的501错误。当我使用正确的'formtoken'值时,我甚至会得到这个。你知道吗

那么,这501的原因是什么?你知道吗


Tags: selfbrowserformhttpapplicationhtmlwwwnl