the responses not work with urllib.request.urlopen
import responses
import requests
from urllib.request import urlopen
@responses.activate
def test_request_img():
url_img = 'http://falseurlf.com'
responses.add(
method=responses.GET,
url=url_img,
body='111',
)
resp = urlopen(url_img) #not work
#resp = requests.get(url_img) #work
return resp
resp = test_request_img()
It got the following error
---------------------------------------------------------------------------
RemoteDisconnected Traceback (most recent call last)
<ipython-input-8-2105ab8f8843> in <module>
14 #resp = requests.get(url_img) #work
15 return resp
---> 16 resp = test_request_img()
<string> in wrapper()
<ipython-input-8-2105ab8f8843> in test_request_img()
11 body='111',
12 )
---> 13 resp = urlopen(url_img) #not work
14 #resp = requests.get(url_img) #work
15 return resp
~/anaconda3/lib/python3.8/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
220 else:
221 opener = _opener
--> 222 return opener.open(url, data, timeout)
223
224 def install_opener(opener):
~/anaconda3/lib/python3.8/urllib/request.py in open(self, fullurl, data, timeout)
523
524 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
--> 525 response = self._open(req, data)
526
527 # post-process response
~/anaconda3/lib/python3.8/urllib/request.py in _open(self, req, data)
540
541 protocol = req.type
--> 542 result = self._call_chain(self.handle_open, protocol, protocol +
543 '_open', req)
544 if result:
~/anaconda3/lib/python3.8/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
500 for handler in handlers:
501 func = getattr(handler, meth_name)
--> 502 result = func(*args)
503 if result is not None:
504 return result
~/anaconda3/lib/python3.8/urllib/request.py in http_open(self, req)
1377
1378 def http_open(self, req):
-> 1379 return self.do_open(http.client.HTTPConnection, req)
1380
1381 http_request = AbstractHTTPHandler.do_request_
~/anaconda3/lib/python3.8/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1352 except OSError as err: # timeout error
1353 raise URLError(err)
-> 1354 r = h.getresponse()
1355 except:
1356 h.close()
~/anaconda3/lib/python3.8/http/client.py in getresponse(self)
1330 try:
1331 try:
-> 1332 response.begin()
1333 except ConnectionError:
1334 self.close()
~/anaconda3/lib/python3.8/http/client.py in begin(self)
301 # read until we get a non-100 response
302 while True:
--> 303 version, status, reason = self._read_status()
304 if status != CONTINUE:
305 break
~/anaconda3/lib/python3.8/http/client.py in _read_status(self)
270 # Presumably, the server closed the connection before
271 # sending a valid response.
--> 272 raise RemoteDisconnected("Remote end closed connection without"
273 " response")
274 try:
RemoteDisconnected: Remote end closed connection without response
But it works with requests.get
import responses
import requests
from urllib.request import urlopen
@responses.activate
def test_request_img():
url_img = 'http://falseurlf.com'
responses.add(
method=responses.GET,
url=url_img,
body='111',
)
#resp = urlopen(url_img) #not work
resp = requests.get(url_img) #work
return resp
resp = test_request_img()
print(resp.text)
result:
the responses not work with
urllib.request.urlopenIt got the following error
But it works with
requests.getresult: