Creating Python Pinger - Sockets -
ok, changed try , ask more specific question.
i have 2 methods. dooneping creation use sendoneping, written me in aid understand sockets better.
i trying understand if creating socket correctly within dooneping, believe am, not sure if missing specific or better.
thanks
def sendoneping(mysocket, destaddr, id): # header type (8), code (8), checksum (16), id (16), sequence (16) mychecksum = 0 # make dummy header 0 checksum # struct -- interpret strings packed binary data header = struct.pack("bbhhh", icmp_echo_request, 0, mychecksum, id, 1) data = struct.pack("d", time.time()) # calculate checksum on data , dummy header. mychecksum = checksum(header + data) # right checksum, , put in header if sys.platform == 'darwin': # convert 16-bit integers host network byte order mychecksum = htons(mychecksum) & 0xffff else: mychecksum = htons(mychecksum) header = struct.pack("bbhhh", icmp_echo_request, 0, mychecksum, id, 1) packet = header + data mysocket.sendto(packet, (destaddr, 1)) # af_inet address must tuple, not str # both lists , tuples consist of number of objects # can referenced position number within object. def dooneping(destaddr, timeout): icmp = getprotobyname("icmp") # sock_raw more details: http://sock-raw.org/papers/sock_raw # fill in start mysocket = socket.socket(socket.af_inet, socket.sock_stream) #fill in end myid = os.getpid() & 0xffff # return current process sendoneping(mysocket, destaddr, myid) delay = receiveoneping(mysocket, myid, timeout, destaddr) mysocket.close() return delay
consider using scapy instead ping. can scapy running:
pip install scapy
using able ping more easily