我正在尝试使用 Python 中的 Mechanize 库打开一个安全 (https) 网站。当我尝试访问该网站时,服务器关闭连接并引发异常 BadStatusLine。
我尝试使用 addheaders
修改标题属性(property),但没有回应。
import mechanize
br = mechanize.Browser()
print 'opening page ...'
resp = br.open('https://onlineservices.tin.nsdl.com/etaxnew/tdsnontds.jsp') #this one works fine
print 'ok'
print 'opening page 2 ...'
resp = br.open('https://incometaxindiaefiling.gov.in/portal/index.do') #exception raised
print 'ok'
异常(exception):
Traceback (most recent call last): File pydev_imports.execfile(file, globals, locals) #execute the script File "Z:\pyTax\app_test.py", line 22, in resp=br.open('https://incometaxindiaefiling.gov.in/portal/index.do')
File "build\bdist.win32\egg\mechanize_mechanize.py", line 203, in open File "build\bdist.win32\egg\mechanize_mechanize.py", line 230, in _mech_open File "build\bdist.win32\egg\mechanize_opener.py", line 188, in open File "build\bdist.win32\egg\mechanize_http.py", line 316, in http_request File "build\bdist.win32\egg\mechanize_http.py", line 242, in read File "build\bdist.win32\egg\mechanize_mechanize.py", line 203, in open
File "build\bdist.win32\egg\mechanize_mechanize.py", line 230, in _mech_open File "build\bdist.win32\egg\mechanize_opener.py", line 193, in open File "build\bdist.win32\egg\mechanize_urllib2_fork.py", line 344, in _open File "build\bdist.win32\egg\mechanize_urllib2_fork.py", line 332, in _call_chain File "build\bdist.win32\egg\mechanize_urllib2_fork.py", line 1170, in https_open File "build\bdist.win32\egg\mechanize_urllib2_fork.py", line 1116, in do_open File "D:\Python27\lib\httplib.py", line 1031, in getresponse response.begin() File "D:\Python27\lib\httplib.py", line 407, in begin version, status, reason = self._read_status() File "D:\Python27\lib\httplib.py", line 371, in _read_status raise BadStatusLine(line) httplib.BadStatusLine: ''
最佳答案
httplib.BadStatusLineis
s 是 HTTPException
的子类.如果服务器以我们不理解的 HTTP 状态代码响应,则引发。这就是导致你的问题的原因。不过,我对修复并不完全确定,因为您的代码在我的计算机上运行良好。
关于python - 无法使用 Python 中的 Mechanize 连接到安全网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12927516/