Hello! Such a problem, wrote the code to try to send a request to the server, and find out if there is a redirect. It works with all URLs, but yesterday I discovered a strange thing, I can’t connect to this URL: ' http://nl.wikipedia.org/wiki/ISO_3166-1 '

Here is the code:

import httplib2 url = 'nl.wikipedia.org/wiki/ISO_3166-1' connection = httplib2.HTTPConnectionWithTimeout(url) connection.request('HEAD', '/') response = connection.getresponse() if response.status in range(301, 304): if response.getheader('Location').startswith('https://' + url): self._options['href'].replace('http://', 'https://') 

ps What did I miss? Maybe it's better to use some other library? I would be grateful for any answers.

    2 answers 2

    And you seem to be using the library incorrectly. It works like this for me

     >>> url = 'nl.wikipedia.org' >>> connection = httplib2.HTTPConnectionWithTimeout(url) >>> connection.request('HEAD', '/wiki/ISO_3166-1') >>> response = connection.getresponse() >>> response.status 301 

    In general, you can try urllib2 as an alternative

    • This is what I need, thanks a lot, it helped! - Oleg Klimenko

    First of all, you need to specify what specific errors occurred in your code. That it was not necessary to copy the code itself and run. Specifically, getaddrinfo failed , which means that the host IP address could not be found.

    Parameters in HTTPConnectionWithTimeout is the host and port. Host! = URL. For your address, the host is nl.wikipedia.org . And the rest of the ( /wiki/ISO_3166-1 ) is the path. Even in the URL should be explicitly specified protocol (scheme) (http, https. Mailto, ftp) and port (?), But in this case the library will put them down for you. Even in the URL there may be parameters (query) and anchor (fragment) - wiki Also checks for redirects do not make much sense, because Wiki redirects all http requests to port 443 (to https). Accordingly, a complete example looks like this:

     host = 'nl.wikipedia.org' connection = httplib2.HTTPConnectionWithTimeout(host) connection.request('HEAD', "/wiki/ISO_3166-1") 

    There is a great alternative - requests . requests itself divides the URL into a host, port, protocol, etc. Conveniently.

    • Thanks for the help! - Oleg Klimenko