urllib2.urlopen 上 HTTP 400 错误请求响应的语法帮助
我有一段代码可以通过代理服务器并使用网站凭据登录网站。我登录就正常了然后我尝试进入一个页面,我在其中发送了我得到的会话 ID,cookie 似乎 发送得很好,但我收到 HTTP 400 错误请求。请查看我的请求的语法,并让我知道我缺少什么。我真的很感激任何对此的反馈!
预先非常感谢, 伊戈尔
import urllib, urllib2, cookielib
proxy_info = {
'user' : 'myuser',
'pass' : 'mypassword',
'host' : "myproxy.company.com",
'port' : 8080
}
proxy_support = urllib2.ProxyHandler({"http" : "http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info})
cj = cookielib.CookieJar()
cookie_h = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler(debuglevel=1) , cookie_h)
headers={'User-agent' : 'Mozilla/5.0'}
urllib2.install_opener(opener)
url = 'http://www.targetsite.com/LogIn.asp?user_id=&user_p assword=myapppassword'
f = urllib2.urlopen(url)
html = f.read()
print html
url2 = 'http://www.targetsite.com/Main.asp?uid=&sid=3294799 60 HTTP/1.1'
response = urllib2.urlopen (url2)
html2 = response.read()
print html2
我得到了这个:
send: 'GET http://www.targetsite.com/Main.asp?u...&sid=329479960 HTTP/1.1 HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: www.targetsite.com\r\nProxy-Authorization: Basic aWNhcnJlb246YWdqYTEZ\r\nCookie: ASPSESSIONIDAQBASTST=CGDGDKDBEDEAGJJOINKPFGCC\r\nC onnection: close\r\nUser-Agent: Python-urllib/2.7\r\n\r\n'
reply: 'HTTP/1.1 400 Bad Request\r\n'
header: Cache-Control: no-cache
header: Pragma: no-cache
header: Content-Type: text/html; charset=utf-8
header: Proxy-Connection: close
header: Connection: close
header: Content-Length: 730
Traceback (most recent call last):
File "C:\Aptana\myDev\root\nested\LaunchApp.py", line 45, in <module>
response = urllib2.urlopen (url2)
File "C:\PYTHON27\LIB\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\PYTHON27\LIB\urllib2.py", line 400, in open
response = meth(req, response)
File "C:\PYTHON27\LIB\urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "C:\PYTHON27\LIB\urllib2.py", line 438, in error
return self._call_chain(*args)
File "C:\PYTHON27\LIB\urllib2.py", line 372, in _call_chain
result = func(*args)
File "C:\PYTHON27\LIB\urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
I have a piece of code where I log on to a web site - through a proxy server and using web site credentials. I log in just fine. Then I try to get into a page where I sent the session Id I got, the cookies seem
to be sent just fine but I get a HTTP 400 Bad Request. Please see the syntax of my request and let me know what I'm missing. I'd really appreciate any feedback on this!
Thanks a lot in advance,
Igor
import urllib, urllib2, cookielib
proxy_info = {
'user' : 'myuser',
'pass' : 'mypassword',
'host' : "myproxy.company.com",
'port' : 8080
}
proxy_support = urllib2.ProxyHandler({"http" : "http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info})
cj = cookielib.CookieJar()
cookie_h = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler(debuglevel=1) , cookie_h)
headers={'User-agent' : 'Mozilla/5.0'}
urllib2.install_opener(opener)
url = 'http://www.targetsite.com/LogIn.asp?user_id=&user_p assword=myapppassword'
f = urllib2.urlopen(url)
html = f.read()
print html
url2 = 'http://www.targetsite.com/Main.asp?uid=&sid=3294799 60 HTTP/1.1'
response = urllib2.urlopen (url2)
html2 = response.read()
print html2
I get this back:
send: 'GET http://www.targetsite.com/Main.asp?u...&sid=329479960 HTTP/1.1 HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: www.targetsite.com\r\nProxy-Authorization: Basic aWNhcnJlb246YWdqYTEZ\r\nCookie: ASPSESSIONIDAQBASTST=CGDGDKDBEDEAGJJOINKPFGCC\r\nC onnection: close\r\nUser-Agent: Python-urllib/2.7\r\n\r\n'
reply: 'HTTP/1.1 400 Bad Request\r\n'
header: Cache-Control: no-cache
header: Pragma: no-cache
header: Content-Type: text/html; charset=utf-8
header: Proxy-Connection: close
header: Connection: close
header: Content-Length: 730
Traceback (most recent call last):
File "C:\Aptana\myDev\root\nested\LaunchApp.py", line 45, in <module>
response = urllib2.urlopen (url2)
File "C:\PYTHON27\LIB\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\PYTHON27\LIB\urllib2.py", line 400, in open
response = meth(req, response)
File "C:\PYTHON27\LIB\urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "C:\PYTHON27\LIB\urllib2.py", line 438, in error
return self._call_chain(*args)
File "C:\PYTHON27\LIB\urllib2.py", line 372, in _call_chain
result = func(*args)
File "C:\PYTHON27\LIB\urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
url
、url2
无效。尝试:名称
user_p assword
不应包含空格。此外,sid
的值似乎也很可疑。这可能是复制粘贴错误。url
,url2
are invalid. Try:The name
user_p assword
should not probably contain a space. Also the value forsid
seems suspicious. It might be a copy-paste error.为什么要在第二个 URL 中发送“HTTP/1.1”?该语法似乎无效,并且 ulrlib2 无论如何都会发送 HTTP/1.1。从痕迹来看,这似乎就是问题所在。
Why are you sending "HTTP/1.1" in your second URL? That syntax doesn't seem to be valid, and ulrlib2 sends HTTP/1.1 anyway. From the trace, this looks to be where the problem lies.