It's not possible to get an answer to URL, although the browser comes in without a problem.



  • Hello, everyone! That's a problem, wrote a code to try to send a request to the server, and to find out if there's a recount. He's working with all the URL, but yesterday, he found a strange thing, I can't connect to this URL: ' http://nl.wikipedia.org/wiki/ISO_3166-1 '

    Here's 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://')

    p.s. What did I miss? Maybe we should use some other library.
    I'll be grateful for any answers.



  • First of all, we need to point out what kind of mistakes you have in the code. So you don't have to copy and start the code. It's definitely out here. getaddrinfo failedwhich means that the IP address of the host cannot be found.

    The HTTPConnectionWithTimeout parameters are. host and port. Host! For your address, the host is nl.wikipedia.org♪ And the rest of it./wiki/ISO_3166-1is the path (path). Also in the URL, protocol(scheme) should be clearly indicated (http://s. mailto, ftp) and port(?), but in this case the library will be for you. Parameters (query) and anchor (fragment) - may be present in URL. https://ru.wikipedia.org/wiki/URL Also, checks on the recirct make little sense, because Wiki redirects all the queries to 443 ports (at https). The perfect example is:

    host = 'nl.wikipedia.org'
    

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

    There's a beautiful alternative-- http://docs.python-requests.org/en/master/ . requests divide URL into the host, port, protocol, etc. Suitable.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2