为了账号安全,请及时绑定邮箱和手机立即绑定

http.client.InvalidURL: nonnumeric port:

http.client.InvalidURL: nonnumeric port:

杨魅力 2022-08-02 16:13:50
我一直在尝试制作一个脚本来检查是否存在随机网站,如果它确实存在,则打开它,但我不断收到一堆不同的错误。这是我的代码:import webbrowserimport timeimport randomimport http.clientfrom random_word import RandomWordsr=RandomWords()while True:    possible_things = random.choice([".com",".net"])    WEB = "http://"+r.get_random_word()+possible_things    c = http.client.HTTPConnection(WEB)    if c.getresponse().status == 200:        seconds = random.randint(5,20)        print("Web site exists; Website: "+WEB+" ; Seconds: "+seconds)        time.sleep(seconds)        webbrowser.open(WEB)        print("Finished countdown, re-looping...")    else:        print('Web site DOES NOT exists; Website: '+WEB+'; re-looping...')这是错误:Traceback (most recent call last):  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 877, in _get_hostport    port = int(host[i+1:])ValueError: invalid literal for int() with base 10: '//water-lined.net'During handling of the above exception, another exception occurred:Traceback (most recent call last):  File "Troll.py", line 10, in <module>    c = http.client.HTTPConnection(WEB)  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 841, in __init__    (self.host, self.port) = self._get_hostport(host, port)  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 882, in _get_hostport    raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])http.client.InvalidURL: nonnumeric port: '//water-lined.net'
查看完整描述

2 回答

?
慕码人8056858

TA贡献1803条经验 获得超6个赞

我只是设置了很长的延迟并添加了很多尝试,但是非常感谢@B łotosmętek!


查看完整回答
反对 回复 2022-08-02
?
郎朗坤

TA贡献1921条经验 获得超9个赞

WEB = "http://"+r.get_random_word()+possible_things

c = http.client.HTTPConnection(WEB)

在第一行中,您创建一个 URL,从 http:// 在第二行中,您将它传递给一个函数,该函数不需要 URL,而是一个具有可选和端口号的主机名。由于您的字符串在“http”之后包含冒号,因此“http”将被假定为主机名,冒号之后的所有内容,即'//something.tld'被解释为端口号 - 但它不能转换为整数,因此错误。:


你可能想做的是沿着以下路线:


host = r.get_random_word() + possible_things

WEB = 'http://' + host

c = http.client.HTTPConnection(host)

c.request('GET', '/')

resp = c.getresponse()

if resp.status == 200:

等。


查看完整回答
反对 回复 2022-08-02
  • 2 回答
  • 0 关注
  • 551 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号