Request a web starting with a hyphen in python - python

Python
I need to request some urls with python. I'm using standard urllib2 procdeure, for instance:
urllib2.urlopen('http://google.com')
Works with no problem. However when using a url that starts with a dash, for instance:
urllib2.urlopen('http://-cityoflove.tumblr.com/')
The script brokes with the error:
urllib2.URLError: <urlopen error [Errno -2] Name or service not known>
Does anyone could thing on a way to get the domain from python?
Ubuntu
If could be help full note that same error is happening with wget:
wget http://-cityoflove.tumblr.com/
That crashes with error:
wget: unable to resolve host address `-cityoflove.tumblr.com'
Note that this error is notified on ubuntu bugs: https://bugs.launchpad.net/ubuntu/+source/resolvconf/+bug/668926

Related

WinError 10060 error in Jupyter python coding when I can access a URL and its csv file in browser directely

I have a strange problem when I use Jupyter for python programming, I can access a URL and the corresponding csv file in my browser directely, but I get an error when reading it with the following command [WinError 10060].
For example:
df_rating=pd.read_csv("https://frankxu1987.weebly.com/uploads/6/2/5/8/62583677/acf351b_ratings.csv")
and the error message is:
URLError: <urlopen error [WinError 10060] (Connection attempt failed because the connecting party did not reply correctly after some time or the connected host did not respond)
(I am using a Chinese environment in win 10, if this could help. Also, I am using VPN but this error occurs with or without VPN)
I tried start and shut down VPN for many times but it didn't help anyway. Could anyone tell me the reason? Much thanks.

How to download an image properly from the web using 'urllib.request' in Python 3.7?

I tried to download an image from the web using Python 3.7. But I got some error in my code and I cannot understand what is wrong in my code and how to recover it.
I use PyCharm 3.4 and MacOS X:
My Code:
import urllib.request
urllib.request.urlretrieve("http://www.digimouth.com/news/media/2011/09/google-logo.jpg", "local-filename.jpg")
Error
urllib.error.URLError: <urlopen error [Errno 65] No route to host>
Your approach is correct. However, the link itself is dead; hence the error.
Simply use urllib.request.urlretrieve(url=link, filename=output), your approach is correct. If the url is an image, you download an image. If the url is an HTML file, you download a HTML file.
Your error urllib.error.URLError: <urlopen error [Errno 65] No route to host> is because your link is broken. The urlretrieve only works for non-broken links. Additionally, urlretrieve is considered to belong in the legacy interface.
Unfortunately, there is nothing you can do to fix the URL "http://www.digimouth.com/news/media/2011/09/google-logo.jpg" and it also appears to be suspicious now.
The code you provided works for a different picture, for example:
import urllib.request
urllib.request.urlretrieve("https://www.google.com/url?sa=i&source=images&cd=&cad=rja&uact=8&ved=2ahUKEwi7nsiIqqXgAhUnuqQKHY6uDa4QjRx6BAgBEAU&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FGiraffe&psig=AOvVaw1g8lkjuT8Ly2FxVhGp1vp6&ust=1549481373274429", "giraffe.jpg")
The problem is with your link, as http://www.digimouth.com/news/media/2011/09/google-logo.jpg seems to be dead. Even wget http://www.digimouth.com/news/media/2011/09/google-logo.jpg does not work in terminal and Chrome cannot open that link properly. So I suggest to choose a different image.
For SSL error, see: https://stackoverflow.com/a/28052583/8565438

<urlopen error [Errno 1] _ssl.c:510: error:14077417:SSL

Does anyone know why I am getting this error?
SSLError: [Errno 1] _ssl.c:510: error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1
I get the erro when using requests or urllib2, I'm running the code on Kodi. The code runs fine when I run it on Visual Studio on my PC.
I am trying to scrape a website that is blocked by my ISP, so I'm using a proxy version of the site.
import requests
url = 'https://kickass.unblocked.pe/'
r = requests.get(url)
The site is hosted by Cloudflare Free SSL and requires support for Server Name Indication (SNI). SNI is support with Python 2.7 only since version 2.7.9. I guess that you are using an older version.
verify=False (which is usually a bad idea anyway) will not help here because without SNI the handshake will fail because the server does not know which certificate is requested and thus will not sent any certificate but instead an alert.

Python requests - get request to secured resource - SSL3_GET_RECORD:decryption failed or bad record mac

I am using python-requests to perform get requests to some resources.
In staging and production environments things works out fine, but in the test environment with a slightly different setup, I receive the message below when trying to perform the request:
requests.exceptions.SSLError: [Errno 1] _ssl.c:510: error:1408F119:SSL routines:SSL3_GET_RECORD:decryption failed or bad record mac
I have tried using an adapter as specified here: SSL Error on Python GET Request
I have tried all the protocols in the list. Still no luck.
I have tried mounting both the complete url and the domain url. No difference.
Any ideas on what to try next?

Python liburl2 timeout; can ping server fine, and wget works fine;

I'm trying to use Python's liburl2 to access the Dreamhost API documented here: http://wiki.dreamhost.com/API
Here is my code:
request = urllib2.Request('https://api.dreamhost.com/?key=<key>')
response = urllib2.urlopen(request)
page = response.read()
print(page)
This invariably fails with the error:
urllib2.URLError: <urlopen error [Errno 104] Connection reset by peer>
I'm absolutely stumped, because I can ping api.dreamhost.com just fine, and wget https://api.dreamhost.com/?key= works fine, too.
Any ideas?
I know it's an old question, but I faced the same problem, and found the solution through two other questions.
This, that shows me the problem is with the handshake using SSLv3:
OpenSSL issues in Debian Wheezy
And this, that gives some possible solutions:
Python HTTPS requests (urllib2) to some sites fail on Ubuntu 12.04 without proxy

Categories