Python p2p chat client/server with built-in NAT traversal (UDP hole punching).
I've write an article about the detailed implementation (in Chinese).
Based on
Python edition: py2.6+ but no Python 3 support
Platform: Linux/Windows
Suppose you run server.py on a VPS with ip 1.2.3.4, listening on port 5678
$ server.py 5678
On client A and client B (run this on both clients):
$ client.py 1.2.3.4 5678 100
The number 100
is used to match clients, you can choose any number you like but only clients with the same number will be linked by server. If two clients get linked, two people can chat by typing in terminal, and once you hit <ENTER>
your partner will see your message in his terminal.
Encoding is a known issue since I didn't pay much effort on making this tool perfect, but as long as you type English it will be fine.
You could do simulation testing by specifying a fourth parameter of client.py
, it will assume that your client is behind a specific type of NAT device.
Here are the corresponding NAT type and number:
FullCone 0
RestrictNAT 1
RestrictPortNAT 2
SymmetricNAT 3
So you might run
$ client.py 1.2.3.4 5678 100 1
pretending your client is behind RestrictNAT.
You can test the relay server functionality by making 3
as the forth parameter, since if one client is behind symmetric NAT, there will be no direct connection but server forwaring.
MIT