I've read something about port translation and now I want to test it.
I have a local machine behind a NAT router and a server with external IP address.
This is how I send packet from 5000th port on my machine to 4000th port on the server.
import socket
import sys
UDP_IP = #external server IP address
UDP_PORT = 4000
MESSAGE = "Hi!"
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind(('0.0.0.0', 5000))
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
Right after that I start to listen 5000th on local machine
import socket
import sys
UDP_IP = #my ip address in the local network
UDP_PORT = 5000
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024)
print "received message:", data
On the server when I see incoming UDP from (someIP, somePort)
I send response to the same someIP
and somePort
(use the same scripts with other port and address). But I never receive this response on my local machine. Why?
Also, this code is correctly work when server is in the local network.