【发布时间】:2012-10-17 05:13:37
【问题描述】:
我正在尝试使用 Python 中的 mechanize 库打开一个安全 (https) 网站。当我尝试访问该网站时,服务器会关闭连接并引发异常 BadStatusLine。
我尝试使用addheaders 属性修改标题,但没有响应。
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'
例外:
Traceback(最近一次调用最后一次):文件 pydev_imports.execfile(file, globals, locals) #执行脚本 File "Z:\pyTax\app_test.py", line 22, in resp=br.open('https://incometaxindiaefiling.gov.in/portal/index.do')
文件“build\bdist.win32\egg\mechanize_mechanize.py”,第 203 行,在 打开文件“build\bdist.win32\egg\mechanize_mechanize.py”,第 230 行, 在 _mech_open 文件“build\bdist.win32\egg\mechanize_opener.py”中, 第 188 行,在打开的文件“build\bdist.win32\egg\mechanize_http.py”中, 第 316 行,在 http_request 文件中 “build\bdist.win32\egg\mechanize_http.py”,第 242 行,在读取文件中 “build\bdist.win32\egg\mechanize_mechanize.py”,第 203 行,打开
文件“build\bdist.win32\egg\mechanize_mechanize.py”,第 230 行,在 _mech_open 文件“build\bdist.win32\egg\mechanize_opener.py”,第 193 行,在打开的文件中 “build\bdist.win32\egg\mechanize_urllib2_fork.py”,第 344 行,在 _open 文件“build\bdist.win32\egg\mechanize_urllib2_fork.py”,第 332 行,在 _call_chain 文件“build\bdist.win32\egg\mechanize_urllib2_fork.py”,第 1170 行,在 https_open 文件中 “build\bdist.win32\egg\mechanize_urllib2_fork.py”,第 1116 行,在 do_open 文件“D:\Python27\lib\httplib.py”,第 1031 行,在 getresponse 中 response.begin() 文件“D:\Python27\lib\httplib.py”,第 407 行,开始 版本、状态、原因 = self._read_status() 文件“D:\Python27\lib\httplib.py”,第 371 行,在 _read_status raise BadStatusLine(line) httplib.BadStatusLine: ''
【问题讨论】:
-
mechanize 可能会尝试连接服务器拒绝/不支持的 ssl 协议版本。
-
您的代码在我的电脑上运行良好,是仅在访问此特定网站时才会出现这种情况还是其他网站也会出现这种情况?
-
这个问题只出现在特定网站上,其他网站使用相同的代码也能正常工作。该网站在浏览器中完美打开。
标签: python urllib2 mechanize httplib