Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

httplib.InvalidURL: nonnumeric port:

Tags:

python

file

i'm trying to do a script which check if many urls exists:

import httplib

with open('urls.txt') as urls:
    for url in urls:
        connection = httplib.HTTPConnection(url)
        connection.request("GET")
        response = connection.getresponse()
        if response.status == 200:
            print '[{}]: '.format(url), "Up!"

But I got this error:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    connection = httplib.HTTPConnection(url)
  File "/usr/lib/python2.7/httplib.py", line 693, in __init__
    self._set_hostport(host, port)
  File "/usr/lib/python2.7/httplib.py", line 721, in _set_hostport
    raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
httplib.InvalidURL: nonnumeric port: '//globo.com/galeria/amazonas/a.html

What's wrong?

like image 458
user1985563 Avatar asked Jan 24 '13 00:01

user1985563


3 Answers

This might be a simple solution, here

connection = httplib.HTTPConnection(url)

you are using the httpconnection so no need to give url like, http://OSMQuote.com but instead of that you need to give OSMQuote.com.

In short remove the http:// and https:// from your URL, because the httplib is considering : as a port number and the port number must be numeric,

Hope this helps!

like image 160
Atul Arvind Avatar answered Nov 15 '22 16:11

Atul Arvind


httplib.HttpConnection takes the host and port of the remote URL in its constructor, and not the whole URL.

For your use case, it's easier to use urllib2.urlopen.

import urllib2

with open('urls.txt') as urls:
    for url in urls:
        try:
            r = urllib2.urlopen(url)
        except urllib2.URLError as e:
            r = e
        if r.code in (200, 401):
            print '[{}]: '.format(url), "Up!"
        elif r.code == 404:
            print '[{}]: '.format(url), "Not Found!" 
like image 44
tom Avatar answered Nov 15 '22 17:11

tom


nonnumeric port:

Solution :

http.client.HTTPSConnection("api.cognitive.microsofttranslator.com")

Remove "https://" from Service URL or Endpoint and it will work.

https://appdotpy.wordpress.com/2020/07/04/errorsolved-nonnumeric-port/

like image 37
Friyank Avatar answered Nov 15 '22 17:11

Friyank