【问题标题】:Unable to connect to secure website using mechanize in Python无法使用 Python 中的 mechanize 连接到安全网站
【发布时间】: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


【解决方案1】:

httplib.BadStatusLineisHTTPException 的子类。如果服务器以我们不理解的 HTTP 状态代码响应,则引发。这就是导致你的问题的原因。不过,我并不完全确定修复程序,因为您的代码在我的计算机上运行良好。

【讨论】:

    猜你喜欢
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 2015-04-16
    相关资源
    最近更新 更多