I want to test out an API I created using my localhost.
import requests
def test_post_spread_api():
r = requests.post("https://localhost:8000/api/test")
print(r.status_code)
test_post_spread_api()
However, I get the error:
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /api/misc/spread_monitor (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f8298604940>: Failed to establish a new connection: [Errno 111] Connection refused',))
There is nothing wrong with the API since it works when I use postman.
I am running my localhost using docker with the django framework. Any ideas?
EDIT: Complete traceback:
Traceback (most recent call last):
File "sandbox/test_spread_monitor.py", line 12, in <module>
test_post_spread_api()
File "sandbox/test_spread_monitor.py", line 9, in test_post_spread_api
r = requests.post("https://localhost:8000/api/test")
File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 112, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 508, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /api/misc/spread_monitor (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f2fb9772908>: Failed to establish a new connection: [Errno 111] Connection refused',))
Try changing "https://localhost:8000/api/test" to "http://localhost:8000/api/test".
Related
ua = UserAgent()
headers={
'user-agent':str(ua.random),
'Connection':'close'
}
r = requests.get(url,headers=headers,timeout=5)
I want to scrape some information from a website ,but the function request.get() raise exception occasionally (sometimes successful but sometime not). I've tried many methods, random u-a, timeout, time.sleep, max tries, but of no use.
Is there something wrong with my code, or is it a fault or some anti-scraper system of the website?
Here is the full exception:
Traceback (most recent call last):
File "d:\AAA临时文档\抢课app\爬虫\run2.py", line 7, in <module>
r=requests.get(url=url,headers=headers,timeout=20)
File "C:\Users\86153\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\86153\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\86153\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\86153\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "C:\Users\86153\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\adapters.py", line 504, in send
raise ConnectTimeout(e, request=request)
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='www.dy2018.com', port=443): Max retries exceeded with url: /i/103887.html (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x04046A18>, 'Connection to www.dy2018.com timed out. (connect timeout=20)'))
Python msrestazure(0.4.20V) is throwing below error:
credentials = UserPassCredentials(userid, password)
File "/data/tools/lib/python3.5/site-packages/msrestazure/azure_active_directory.py", line 367, in __init__
self.set_token()
File "/data/tools/lib/python3.5/site-packages/msrestazure/azure_active_directory.py", line 404, in set_token
raise_with_traceback(AuthenticationError, "", err)
File "/data/tools/lib/python3.5/site-packages/msrest/exceptions.py", line 45, in raise_with_traceback
raise error.with_traceback(exc_traceback)
File "/data/tools/lib/python3.5/site-packages/msrestazure/azure_active_directory.py", line 402, in set_token
**optional)
File "/data/tools/lib/python3.5/site-packages/requests_oauthlib/oauth2_session.py", line 221, in fetch_token
verify=verify, proxies=proxies)
File "/data/tools/lib/python3.5/site-packages/requests/sessions.py", line 555, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "/data/tools/lib/python3.5/site-packages/requests_oauthlib/oauth2_session.py", line 360, in request
headers=headers, data=data, **kwargs)
File "/data/tools/lib/python3.5/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/data/tools/lib/python3.5/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/data/tools/lib/python3.5/site-packages/requests/adapters.py", line 502, in send
raise ProxyError(e, request=request)
msrest.exceptions.AuthenticationError: , ProxyError: HTTPSConnectionPool(host='login.microsoftonline.com', port=443): Max retries exceeded with url: /common/oauth2/token (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden',)))
We used correct azure credentials, Any idea?
Edit:
This issue is coming up again, this time from different exception requests.exceptions.ProxyError: HTTPSConnectionPool(host='login.microsoftonline.com', port=443): Max retries exceeded with url: /common/UserRealm/username%40<dns>.onmicrosoft.com?api-version=1.0 (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden',)))
Python : 3.6.5 ( Anaconda)
Operating system: RHEL 6.9
I am always hit by 400 Bad request error while connection attempt by https over proxy. But the same is working fine without http and curl. I have exported https_proxy and http_proxy in linux cli. Anyone advise me what to do?
import requests
session = requests.Session()
session.get("https://pypi.python.org/simple/").content
Traceback (most recent call last):
File "/apps/bitech/python/lib/python3.6/site-packages/urllib3/connectionpool.py", line 595, in urlopen
self._prepare_proxy(conn)
File "/apps/bitech/python/lib/python3.6/site-packages/urllib3/connectionpool.py", line 816, in _prepare_proxy
conn.connect()
File "/apps/bitech/python/lib/python3.6/site-packages/urllib3/connection.py", line 294, in connect
self._tunnel()
File "/apps/bitech/python/lib/python3.6/http/client.py", line 919, in _tunnel
message.strip()))
OSError: Tunnel connection failed: 400 Bad Request
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/apps/bitech/python/lib/python3.6/site-packages/requests/adapters.py", line 440, in send
timeout=timeout
File "/apps/bitech/python/lib/python3.6/site-packages/urllib3/connectionpool.py", line 639, in urlopen
_stacktrace=sys.exc_info()[2])
File "/apps/bitech/python/lib/python3.6/site-packages/urllib3/util/retry.py", line 388, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/ (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 400 Bad Request',)))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
File "/apps/bitech/python/lib/python3.6/site-packages/requests/sessions.py", line 521, in get
return self.request('GET', url, **kwargs)
File "/apps/bitech/python/lib/python3.6/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/apps/bitech/python/lib/python3.6/site-packages/requests/sessions.py", line 640, in send
history = [resp for resp in gen] if allow_redirects else []
File "/apps/bitech/python/lib/python3.6/site-packages/requests/sessions.py", line 640, in
history = [resp for resp in gen] if allow_redirects else []
File "/apps/bitech/python/lib/python3.6/site-packages/requests/sessions.py", line 218, in resolve_redirects
**adapter_kwargs
File "/apps/bitech/python/lib/python3.6/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/apps/bitech/python/lib/python3.6/site-packages/requests/adapters.py", line 502, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/ (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 400 Bad Request',)))
I'm trying to run a python script (that is not mine) which is supposed to connect to an IP address (192.168.1.3) on the LAN.
File "control/insert_to_container.py", line 36, in <module>
res=read_info.open_file()
File "/control/read_info.py", line 21, in open_file
r = requests.get('http://'+line.rstrip('\t')+':491155')
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 508, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.1.3', port=491155): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f8fd9e9b250>: Failed to establish a new connection: [Errno 111] Connection refused',))
I specified the IP address in a config file. Would anybody know what the %09 is in the error HTTPConnectionPool(host=192.168.1.3%09. I wondering if this is the source of the error.
Your variable "line" contains a tab, which is then turned into "%09" by the Requests library.
To fix this, you must first ensure that your "line" variable contains no extraneous characters. The most direct way to achieve this is to use .rstrip()
Example:
r = requests.get('http://'+line.rstrip('\t')+':49155')
I need to call a web requests and Im using Python request module.
I have a requests being served on a machine For ex:55.84.201.228. When I enter it throught the browser it works fine and Im able to view the webpage..
but when I use the requests.get, it does not work..It errors with a socket error..
>>> import requests
>>> r = requests.get('https://55.84.201.228')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 383, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 486, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 378, in send
raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='55.84.201.228', port=443): Max retries exceeded with url: / (Caused by <class 'socket.error'>: [Errno 110] Connection timed out)
How can I fix this issue?
>>>r = requests.get('https://www.cnn.com')
This works fine.
I am making a wild guess here since I can't access your machine but from experience, since you are using https on what I assume is some test server try
r = requests.get('https://55.84.201.228', verify=False)
According to the documentation certificate verification is on by default.