Am I on the right path

A

ADE

Hi everyone:

I was just wondering if this code makes sense to anyone. And if I am
writing it correctly, the program seems to keep crashing. I was also
wondering if anyone had any pointers.

The program is supposed to randomly choose an ip address and try and make a
conection on port 80

Cheers to anyone who helps

# GENERATE AND TRY AND CONNECT TO RANDOM IP ADDRESS

import socket, struct, random
RAND = random.random()
PORT = 80
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# These two functions aren't mine
def dottedQuadToNum(ip):
"convert decimal dotted quad string to long integer"
return struct.unpack('L',socket.inet_aton(ip))[0]

def numToDottedQuad(n):
"convert long int to dotted quad string"
return socket.inet_ntoa(struct.pack('L',n))
# I Think I understand them

HOST = dottedQuadToNum("24.207.19.240")
RAND = random.randrange(HOST)
HOST = numToDottedQuad(RAND)

try:
sock.connect((HOST, PORT))
print "connected to " + HOST + " on",
print PORT
sock.close()
except:
print "could not connect to " + HOST + " on",
print PORT


Cheers

Thank You

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,063
Members
47,670
Latest member
micheljon

Latest Threads

Top