python 3 urllib.request.urlopen无法替代python 2 urllib2.urlopen
我继承了一些python 2代码,它使用python 3 urllib.request.urlopen无法替代python 2 urllib2.urlopen,python,python-3.x,python-2.7,rest,Python,Python 3.x,Python 2.7,Rest,我继承了一些python 2代码,它使用urllib2.urlopen将一些数据发布到本地服务器,我想将其转换为python 3。我最初尝试了requests模块,但当这不起作用时,我又回到了urllib.request.urlopen,因为 urllib.request.urlopen()对应于旧的urllib2.urlopen 两种Python3方法都抛出相同的异常,而Python2代码运行良好 正在运行的python 2代码: >>> request = urllib2.
urllib2.urlopen
将一些数据发布到本地服务器,我想将其转换为python 3。我最初尝试了requests
模块,但当这不起作用时,我又回到了urllib.request.urlopen
,因为
urllib.request.urlopen()对应于旧的urllib2.urlopen
两种Python3方法都抛出相同的异常,而Python2代码运行良好
正在运行的python 2代码:
>>> request = urllib2.Request(url)
>>> request.add_data(data)
>>> response = urllib2.urlopen(request)
>>> response.read()
''
>>> response.close()
python 3等效版本
>>> request = urllib.request.Request(url, data)
>>> response = urllib.request.urlopen(request)
生成以下回溯:
---------------------------------------------------------------------------
RemoteDisconnected Traceback (most recent call last)
<ipython-input-56-d5c6f6ac8b5c> in <module>()
----> 1 response = urllib.request.urlopen(request)
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
221 else:
222 opener = _opener
--> 223 return opener.open(url, data, timeout)
224
225 def install_opener(opener):
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
524 req = meth(req)
525
--> 526 response = self._open(req, data)
527
528 # post-process response
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py in _open(self, req, data)
542 protocol = req.type
543 result = self._call_chain(self.handle_open, protocol, protocol +
--> 544 '_open', req)
545 if result:
546 return result
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
502 for handler in handlers:
503 func = getattr(handler, meth_name)
--> 504 result = func(*args)
505 if result is not None:
506 return result
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py in http_open(self, req)
1344
1345 def http_open(self, req):
-> 1346 return self.do_open(http.client.HTTPConnection, req)
1347
1348 http_request = AbstractHTTPHandler.do_request_
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1319 except OSError as err: # timeout error
1320 raise URLError(err)
-> 1321 r = h.getresponse()
1322 except:
1323 h.close()
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py in getresponse(self)
1329 try:
1330 try:
-> 1331 response.begin()
1332 except ConnectionError:
1333 self.close()
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py in begin(self)
295 # read until we get a non-100 response
296 while True:
--> 297 version, status, reason = self._read_status()
298 if status != CONTINUE:
299 break
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py in _read_status(self)
264 # Presumably, the server closed the connection before
265 # sending a valid response.
--> 266 raise RemoteDisconnected("Remote end closed connection without"
267 " response")
268 try:
RemoteDisconnected: Remote end closed connection without response
---------------------------------------------------------------------------
RemoteDisconnected回溯(最近一次呼叫上次)
在()
---->1 response=urllib.request.urlopen(请求)
/urlopen中的Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py(url、数据、超时、cafile、capath、cadefault、上下文)
221其他:
222开瓶器=_开瓶器
-->223返回opener.open(url、数据、超时)
224
225 def安装开启器(开启器):
/打开时的Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py(self、fullurl、data、timeout)
524要求=方法(要求)
525
-->526响应=自身打开(请求,数据)
527
528#过程后响应
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py in_open(self、req、data)
542协议=请求类型
543结果=self.\u调用\u链(self.handle\u打开,协议,协议+
-->544'_open',请求)
545如果结果:
546返回结果
/库/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py in_call_chain(self、chain、kind、meth_name、*args)
502对于处理程序中的处理程序:
503 func=getattr(处理程序,方法名称)
-->504结果=函数(*args)
505如果结果不是无:
506返回结果
/http_open(self,req)中的Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py
1344
1345 def http_打开(自身,请求):
->1346返回self.do_open(http.client.HTTPConnection,req)
1347
1348 http_请求=AbstractHTTPHandler.do_请求_
/do_open中的Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py(self,http_类,req,**http_conn_参数)
1319除OSError as err外:#超时错误
1320错误(err)
->1321 r=h.getresponse()
1322除:
1323 h.关闭()
/getresponse(self)中的Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py
1329尝试:
1330尝试:
->1331响应。开始()
1332除连接错误外:
1333自我关闭()
/begin(self)中的Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py
295#阅读,直到我们得到非100的回复
296虽然正确:
-->297版本,状态,原因=self.\u读取\u状态()
298如果状态!=继续:
299休息
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py处于读取状态(self)
264#大概是服务器之前关闭了连接
265#发送有效响应。
-->266升起远程断开连接(“远程端关闭连接,无连接”
267“答复”)
268试试:
RemoteDisconnected:远程端关闭连接,无响应
如果可能的话,我真的希望避免将现有代码转换为python 2。我无法控制服务器如何处理请求。使用请求模块。还有,这是一篇文章还是一篇文章?您试图传递什么样的数据?请求模块抛出相同的底层异常,正如我在问题顶部提到的。数据是字节编码的字符串。它应该是POST,但我认为这是由
urlib2
或urlib.request
模块决定的。您是否尝试过requests.POST(url,data=yourdata)
?是的,在使用请求时,它会抛出相同的底层异常。异常是:ConnectionError:('Connection aborted',RemoteDisconnected('Remote end closed Connection without response',)