1

I've been trying to make a script to check if a random website exists and then opens it if it does exist, but I keep getting a bunch of different errors. Here is my code:

import webbrowser
import time
import random
import http.client
from random_word import RandomWords
r=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...')

And here is the error:

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'
Ge1p
  • 35
  • 2
  • 7

2 Answers2

0
WEB = "http://"+r.get_random_word()+possible_things
c = http.client.HTTPConnection(WEB)

In the first of those lines you create an URL, beginning with http:// In the second one you pass it to a function that does not expect an URL, but rather a hostname with optional : and port number. Since your string contains a colon after 'http", 'http' will be assumed to be the host name and everything after the colon, ie '//something.tld' is interpreted as a port number - but it cannot be converted to an integer, hence error.

What you probably wanted to do is something along these lines:

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:

etc.

Błotosmętek
  • 12,717
  • 19
  • 29
0

I just set a long delay and added a lot of tries, but thanks a lot @Błotosmętek!

Ge1p
  • 35
  • 2
  • 7