I've been trying for two weeks to create a Python script that will communicate with itself even if run from different LANs.
But I can not perform the NAT traversal. I've tried to check what's going wrong and at least the socket isn't timeout
ing anymore but I can't receive the data.
I think the issue might be that the NAT is mapping the send and receive to different port but don't know how to check that or fix it if it is really the issue.
Code:
import socket
import time
from threading import Thread
PORT = 4206
peers = [''] # public ip adresses
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
LOCAL_HOST = ('127.0.0.1', PORT)
def main():
print('P2P are set to PORT :', PORT)
Thread(target=nat_traversal).start()
def recv():
time.sleep(5)
while True:
print('response:', sock.recvfrom(4096))
def sec30():
for i in range(0, 29):
time.sleep(1)
print(i, end=',')
time.sleep(1)
print('30')
def nat_traversal():
Thread(target=recv).start()
while True:
t = Thread(target=sec30, args=())
t.start()
for peer in peers:
if peer != '':
try:
sock.sendto(b'Hey', (peer, PORT))
except TimeoutError:
print('timeout')
t.join()
if name == 'main':
main()
Can you give me examples of a Python script that performs nat traversal and can send and receive messages?
Thanks in advance (: