2013-06-25 21:19:01 +08:00
|
|
|
#basic python UDP script
|
|
|
|
#for testing only
|
|
|
|
import socket
|
2013-06-26 00:11:11 +08:00
|
|
|
import random
|
2013-06-25 21:19:01 +08:00
|
|
|
|
|
|
|
UDP_IP = "127.0.0.1"
|
|
|
|
UDP_PORT = 5004
|
|
|
|
|
|
|
|
sock = socket.socket(socket.AF_INET, # Internet
|
|
|
|
socket.SOCK_DGRAM) # UDP
|
|
|
|
sock.bind((UDP_IP, UDP_PORT))
|
|
|
|
|
2013-06-26 00:11:11 +08:00
|
|
|
#our client_id
|
|
|
|
client_id = str(''.join(random.choice("abcdefghijklmnopqrstuvwxyz") for x in range(32)))
|
|
|
|
|
|
|
|
print client_id
|
2013-06-29 10:58:46 +08:00
|
|
|
a = 1;
|
2013-06-25 21:19:01 +08:00
|
|
|
#send ping request to our DHT on localhost.
|
2013-06-26 00:11:11 +08:00
|
|
|
sock.sendto("0012345678".decode("hex") + client_id, ('127.0.0.1', 33445))
|
2013-06-25 21:19:01 +08:00
|
|
|
|
2013-08-09 19:57:05 +08:00
|
|
|
#print all packets received and respond to ping requests properly
|
2013-06-25 21:19:01 +08:00
|
|
|
while True:
|
|
|
|
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
|
2013-06-26 00:11:11 +08:00
|
|
|
print "received message:", data.encode('hex'), " From:", addr
|
2013-08-09 19:57:05 +08:00
|
|
|
#if we receive a ping request.
|
2013-06-26 00:11:11 +08:00
|
|
|
print data[0].encode('hex')
|
|
|
|
if data[0] == "00".decode('hex'):
|
|
|
|
print "Sending ping resp"
|
|
|
|
sock.sendto("01".decode('hex') + data[1:5] + client_id, addr)
|
|
|
|
|
2013-08-09 19:57:05 +08:00
|
|
|
#if we receive a get_nodes request.
|
2013-06-26 00:11:11 +08:00
|
|
|
if data[0] == "02".decode('hex'):
|
|
|
|
print "Sending getn resp"
|
|
|
|
#send send nodes packet with a couple 127.0.0.1 ips and ports.
|
|
|
|
#127.0.0.1:5000, 127.0.0.1:5001, 127.0.0.1:5002
|
|
|
|
sock.sendto("03".decode('hex') + data[1:5] + client_id + ("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" + "7F00000113880000".decode('hex') + "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" + "7F00000113890000".decode('hex') + "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" + "7F000001138A0000".decode('hex')), addr)
|
2013-06-29 10:58:46 +08:00
|
|
|
|
|
|
|
if data[0] == "10".decode('hex'):
|
|
|
|
print "Sending handshake resp"
|
|
|
|
sock.sendto("10".decode('hex') + data[1:5] + client_id[:4], addr)
|
|
|
|
if data[0] == "11".decode('hex'):
|
|
|
|
print "Sending SYNC resp"
|
|
|
|
a+=1
|
|
|
|
sock.sendto("11".decode('hex') + chr(a) + data[1:9], addr)
|
2013-06-26 00:11:11 +08:00
|
|
|
|