How to use request in pythonanythere - python

I tried
import requests
url='https://www.cnn.com/'
r=requests.get(url)
Error:
ProxyError: HTTPConnectionPool(host=... port=80): Max retries exceeded with...
The following
print requests.utils.get_environ_proxies('http://www.google.com')
output:
http://proxy.server:3128
What can I do?
Thanks.
Jerry

www.cnn.com is not on the whitelist of sites that free accounts can access. To be included in that whitelist, a site needs to have a publicly documented API.

Related

Python ERROR SSL: WRONG_VERSION_NUMBER on code that worked few days earlier

Using google-search package (https://pypi.org/project/googlesearch-python/) to open few google search results straight from cmd. Worked fine few days ago.
Now that trying to launch the code I get SSL: WRONG_VERSION_NUMBER Error
Any ideas how to fix? And if ideas what caused the error that would be interesting.
Code:
from googlesearch import search
import webbrowser, sys
searching_for = input(("Input search words: "))
num_results = int(input("How many results : ") or "3")
result = search(searching_for)
for i in result[:num_results]:
webbrowser.open(i)
ERROR
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: /search?q=tim+ferriss&num=11&hl=en (Caused by ProxyError('Your proxy appears to only use HTTP and not HTTPS, try changing your proxy URL to be HTTP. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#https-proxy-error-http-proxy', SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)'))))
I was able to reproduce the error and fix it using the following trick.
try changing the next line of code to this:
result = search(searching_for,proxy="http")
Caused by ProxyError('Your proxy appears to only use HTTP and not HTTPS, try changing your proxy URL to be HTTP. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#https-proxy-error-http-proxy',
It says that you can try using HTTP on the URL. But the search library that you are using, I don't see any options for changing the protocol........

How to check if website is up or down in python

I'm creating script which will tell us is website is down or up. But I got error HTTPSConnectionPool(host='gooogle.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError("hostname 'gooogle.com' doesn't match 'www.google.com'")))
here is my code:
import requests
website_url = input("Enter a website url: ")
r = requests.get(f'https://{website_url}', timeout=5)
if r.status_code != 200:
print(f"uh no, {website_url} is down")
else:
print(f"Good News, {website_url} is up")
I am using the requests module for this. To be honest, I don't know how to check the website is down or up but still, I write this code if you know how please answer me with details.
First of all, you're referring to https://www.gooogle.com instead of https://www.google.com; which I assume is the webpage that you want to test.
Regarding the reason of your error:
Since you're accessing with the HTTPS protocol, you will check the certificates of the destination server, in order to verify that this one is who is supposed to be.
Apparently, the domain https://www.gooogle.com is offering the same certificate than https://www.google.com, and also it performs a redirect to it, but that domain isn't included in the Subject Alternate Name (SAN) of the Google certificate, so actually you cannot verify the authenticity of the accessed domain.
If you use your code against a site that has correctly set the SSL verification, it should work without problems. On another hand, if you just want to check if a site is UP or DOWN independently on its SSL configuration, you can just pass the flag verify set to False:
requests.get(f'https://{website_url}', verify=False, timeout=5)

How to proxy for OpenWeatherMap API

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 ...

Python https-proxy request Entsoe

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).

Python requests API using proxy for https request get 407 Proxy Authentication Required

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.

Categories