我正在尝试做一个检查是否存在多个urls的脚本:
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!"但是我得到了这个错误:
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怎么了?
发布于 2013-01-24 08:27:18
httplib.HttpConnection在其构造函数中接受远程URL的host和port,而不是整个URL。
对于您的用例,使用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!" 发布于 2013-08-30 13:51:20
这可能是一个简单的解决方案,这里
connection = httplib.HTTPConnection(url)您使用的是httpconnection,所以不需要提供http://OSMQuote.com之类的url,而是需要提供OSMQuote.com。
简而言之,从URL中删除http://和https://,因为httplib将:视为端口号,并且端口号必须是数字,
希望这能有所帮助!
发布于 2020-07-04 23:08:36
非数字端口:
解决方案:
http.client.HTTPSConnection("api.cognitive.microsofttranslator.com")
从服务https://“或端点中删除” URL“,它就可以工作了。
https://appdotpy.wordpress.com/2020/07/04/errorsolved-nonnumeric-port/
https://stackoverflow.com/questions/14491814
复制相似问题