0

I wrote this python code to open sockets but I can't figure out how to check if a URL exists or not

inp = input('Enter a URL: ')
ipt = inp.split('/')
inu = ipt[2]

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((inu, 80))
cmd = 'GET x HTTP/1.0\r\n\r\n'
mmd = cmd.replace('x',inp)
bmd = mmd.encode()
mysock.send(bmd)

while True:
    data = mysock.recv(512)
    if len(data) < 1:
        break
    print(data.decode(),end='')

mysock.close()
TechDash
  • 7
  • 3
  • 8

1 Answers1

1

There are a few ways to do this. One would be to use the validator module. It would look something like this:

import validators

valid=validators.url('https://codespeedy.com/')
if valid==True:
    print("Url is valid")
else:
    print("Invalid url")

Another way would be to use the requests module, like this:

import requests

try:
    response = requests.get("http://www.google.com/")
    print("URL is valid and exists on the internet")
except requests.ConnectionError as exception:
    print("URL does not exist on Internet")
Nimantha
  • 6,405
  • 6
  • 28
  • 69
The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24