I was wondering if I can get some help with a problem I'm having.
I'm using requests to pull some data from an api for a web app that I'm deploying on Heroku. On my local computer I have no problem connecting to the API and pulling data down, but when I deploy to Heroku it won't work. (The same thing happens when I try testing on Cloud 9 IDE)
Example Code below:
import requests
email = 'example#example.com'
url = 'https://api.example.com/'
auth = ('example', 'example')
headers = {'Accept': 'application/json', 'content-type': 'application/json'}
params = {'emailaddress': email}
r = requests.get(url+'customer.svc/search', params=params, auth=auth, headers=headers)
It's very basic, but I just can not get it to work.
Everytime I try to connect I get the error:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.example.com', port=443): Max retries exceeded with url: /customer.svc/search?emailaddress=example%40example.com (Caused by <class 'socket.error'>: [Errno 104] Connection reset by peer)
I'm new to Heroku, so this might be prety basic. What am I missing?
Edit:
It seems the error only happens when I deploy my code. I've tried the code on several machines and I have no trouble with the request. I wonder if it has something to do with the connection back to my Heroku app?
Related
I am using the response module of python to download quite some data but I am facing an issue. Before posting my specific case, I have tried with a simpler code to test the issue and I face the same problem. Basically, when running this:
import request
x = requests.get('https://w3schools.com')
print(x.status_code)
following is the error I am facing.
ConnectionError: HTTPSConnectionPool(host='w3schools.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002240C195760>: Failed to establish a new connection: [Errno 11002] getaddrinfo failed'))
Could anyone guide me on this. I suspect its an issue with my network configuration? I have no experience with network management so I am lost on that.
Thanks
Well, I just found out it is indeed an error with my network, basically missing the proxy info as I am working behind one.
For those looking at this in the future, add your proxy settings manually:
import requests
proxies = {'http': 'http://your.proxy.com:8080',
'https': 'http://your.proxy.com:8080'}
url = 'https://w3schools.com'
response = requests.post(url, proxies=proxies)
print(response.status_code)
If you get 200 then it is working.
i have a program and i want to show the current weather information in the corner. The code works at my own computer but at my work notebook i cant establish connection and i get this error:
requests.exceptions.ProxyError:
HTTPSConnectionPool(host='api.openweathermap.org', port=443): Max
retries exceeded with url:
/data/2.5/weather%5Bhttps://api.openweathermap.org/data/2.5/weather%5D?APPID=abc123
d88&q=Frankfurt&units=metric (Caused by ProxyError('Cannot connect to
proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection
object at 0x000002563074FB48>: Failed to establish a new connection:
[WinError 10060]
I found this as a solution and i sounds promising:
Using an HTTP Proxy
Unforutnately i dont get it how i need to implement it because i dont have knowledge about the internet settings of the company. Something similar to this:
response = requests.get(url, params=params, proxies={"http": "12.34.56.78:1234", "https": "12.34.56.78:1234"})
what i do know is that i need to set this in PyCharm's Terminal to use pip install, and it seems related:
set HTTP_PROXY=12.34.56.78:1234
set HTTPS_PROXY=12.34.56.78:1234
I dont understand much about network settings but with that it works. Would i have to do this for the program as well? The port of the error message (443) is not matching to the port i enter above (1234).
Can you help me here? Would be much apreciated! :-)
I solved it:
i defined this globally:
proxies = {'http':'http://12.34.45.67:1234','https':'http://12.34.45.67:1234'}
And made my request like this:
response = requests.get(url, params=params, proxies=proxies)
I had some typos in the code before, which didnt help also ...
I am behind a proxy settings with the environment variables on the proxy settings set properly. The environment variables are working properly when doing pip installs for instance. I retrieve the proxy settings with the getproxies method, which I checked and returns the correct dict.
I am trying the following approach:
import requests
import urllib
r = requests.get('http://www.nu.nl', proxies=urllib.request.getproxies())
The error message I get:
ProxyError: HTTPSConnectionPool(host='www.nu.nl', port=443): Max retries
exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.',
OSError('Tunnel connection failed: 407 authenticationrequired',)))
What am I missing here?
Ok, figured it out. The issue with my environment variables was that the username and password were not specified because I work in a single sign on environment. Therefore the password needs to be properly set. Just substituting my credentials in the proxy definition did not work, so I had to use the urllib opener to fix my issue. Now it works like a charm.
import urllib
username = 'userID' # ex. ID
password = "password" # password
targetUrl = "http://www.example.org/"
proxies = {
'https': 'https://{}:{}#proxyAdress:port'.format(username, password)}
proxy = urllib.request.ProxyHandler(proxies)
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)
with urllib.request.urlopen(targetUrl) as url:
text = str(url.read())
The HTTP 407 code states that you need to authenticate to the proxy server.
The http response header Proxy-Authenticate will tell you what kind of authentication is required - print out the response headers.
It might also be a problem with the url...try removing the www bit
For reference : https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/407
I'm trying to fetch data from Entsoe's transparency web-API on my server which is behind a proxy. However, when I run the following code I keep getting a bad handshake error. The web-API does connect from the browser.
import requests
url='https://transparency.entsoe.eu/api'
proxies = {'http':'172.26.28.25:8080', 'https':'172.26.28.25:8080'}
requests.get(url, proxies = proxies)
I keep getting error:
requests.exceptions.SSLError: HTTPSConnectionPool(host='transparency.entsoe.eu', port=443): Max retries exceeded with url: /api?... (Caused by SSLError(SSLError("bad handshake: SysCallError(10054, 'WSAECONNRESET')",),))
How can I solve this error?
Kind regards,
Matthijs Toorenburg
If you want to initiate an API call you should supply the request parameters too.
W/o an API token Rejected TP request you'd only get Unauthorized (HTTP: 401).
You should also keep in mind, that the TP allows only a limited number of requests posted from a single IP within a specific timeframe. Id. TP query limits
In other cases, you address the host of the API, without acutally initiating an API call
(you could use API Tester as a toy example).
I've been struggling with my company proxy to make an https request.
import requests
from requests.auth import HTTPProxyAuth
proxy_string = 'http://user:password#url_proxt:port_proxy'
s = requests.Session()
s.proxies = {"http": proxy_string , "https": proxy_string}
s.auth = HTTPProxyAuth(user,password)
r = s.get('http://www.google.com') # OK
print(r.text)
r = s.get('https://www.google.com',proxies={"http": proxy_string , "https": proxy_string}) #OK
print(r.text)
r = s.get('https://www.google.com') # KO
print(r.text)
When KO, I have the following exception :
HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',)))
I looked online but didn't find someone having this specific issue with HTTPS.
Thank you for your time
Thanks to the amazing help of Lukasa, I solved my issue.
Please see discussion on fix here
or set :
session.trust_env=False
I personally solved the above problem on my system by updating the environment variables http_proxy,https_proxy,socks_proxy,ftp_proxy.
First enter the command on your terminal : printenv
This should show you the environment variables on your system.
In my case intially:
http_proxy=http://proxyserver:port/
I changed it to : http_proxy=http://username:password#proxy:port/
using the command
export http_proxy="http://username:password#proxy:port/"
Similarly for https_proxy,socks_proxy,ftp_proxy
Other way i have resolved is - speak with your corporate IT administrator and find a direct proxy port which connects to external domain (with / without password)
pip install --proxy=http://proxyhost:proxy_port pixiedust
Found from other colleagues using the proxy (proxy_port direct connection) in their eclipse settings (network)
To anyone else that tried the accepted answer's "session.trust_env=False" with no success, there may be a deeper issue that produces a similar error (which is probably not the issue the OP had): There may be a corporate proxy configuration that requires specific headers to be sent upon CONNECT, and python requests doesn't send them ('User-Agent' and 'Host', for example).
I do not have a solution for that at the moment. See https://github.com/psf/requests/issues/5028 for a discussion on the subject.