Reputation: 31
I tried to send a python request to a proxy but it failed. What did I do wrong?
Here's the code:
import requests
url = 'https://httpbin.org/ip'
proxies = {
"http": 'http://103.250.153.242',
"https": 'http://103.250.153.242'
}
response = requests.get(url,proxies=proxies)
print(response.json())
The result should have returned {'origin': '103.250.153.242'} but instead I got this:
Traceback (most recent call last):
File "C:\Python37\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw)
File "C:\Python37\lib\site-packages\urllib3\util\connection.py", line 80, in create_connection
raise err
File "C:\Python37\lib\site-packages\urllib3\util\connection.py", line 70, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
How do I fix this?
Upvotes: 3
Views: 9731
Reputation: 1
You have these proxies:
proxies = {
"http": 'http://103.250.153.242',
"https": 'http://103.250.153.242'
}
But it's not correct because the IP has no port number, just add the port you have for this ip, no other port. It will be something like this:
proxies = {
"http": 'http://103.250.153.242:PORT',
"https": 'http://103.250.153.242:PORT'
}
Upvotes: 0
Reputation: 2137
import urllib
import requests
r = requests.get('http://google.com', proxies=urllib.request.getproxies())
But if you have proxies list:
from urllib3 import ProxyManager, make_headers
default_headers = make_headers(proxy_basic_auth='myusername:mypassword')
http = ProxyManager("https://myproxy.com:8080/", headers=default_headers)
# Now you can use `http` as you would a normal PoolManager
r = http.request('GET', 'https://stackoverflow.com/')
Or:
import requests
proxies = {
'http': 'http://proxy.server:port',
'https': 'http://proxyserver:port',
}
s = requests.Session()
s.proxies = proxies
r = s.get('https://api.ipify.org?format=json').json()
Upvotes: 1