Iβm trying to get a random selection of web pages, I donβt want to give up Google search results for various reasons. Here is how I tried it:
import socket
from random import randint
def doesitserveawebpage(ip):
ip=str(ip)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, 80))
s.shutdown(2)
return True
except:
return False
def givemerandomwebsite():
adrformat = "%d.%d.%d.%d"
while True:
adr = adrformat % tuple(randint(0,255) for _ in range(4))
try:
print "Tring %s" % adr
name = socket.gethostbyaddr(adr)
if (doesitserveawebpage(adr)):
return name
else:
continue
except socket.herror:
continue
Well, that doesn't work. Firstly, it is too slow. Secondly, it gives me addreses that do not serve web pages. Anyway, can I make this code better, or will you suggest another way to solve this problem?
yasar source
share