Python Forum

Full Version: get sender IP on UDP server?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello ,
I want to build my own syslog server
I have created a simple Udp server :
import logging
import socket

if __name__ == '__main__':
    UDP_IP = "10.0.0.111"
    UDP_PORT = 5005
    sock = socket.socket(socket.AF_INET,  socket.SOCK_DGRAM)
    sock.bind((UDP_IP, UDP_PORT))
    while True:
        data, addr = sock.recvfrom(1024)  # buffer size is 1024 bytes
        print("From: %s received message: %s" % (addr, data))
when the message is comming I can see this :
From: ('10.0.0.1', 42060) received message: b'script,error this is error test message'
now the '10.0.0.1' IP is the IP of my router and not from the computer how is sending this message
how can I get the IP

*** up until now I have used Splunk(I don't need ot because it have to much on it - and i just need simple things ) , and with the same error message it does show my the remote\client\sender IP
so I guss it is posiable to get

Thanks ,
Looks okay to me. I wonder if you have some strange networking going on. Unprompted UDP like this shouldn't be subject to NAT, but I have no other explanation. It works fine on my machine. The only change I made was to bind to all addresses so I didn't have to hardcode my IP.

...
if __name__ == '__main__':
    UDP_IP = "0.0.0.0"
    UDP_PORT = 5005
...
Client:
Output:
$ ip -br addr show dev eth0 scope global eth0 UP 10.0.0.10/24 $ echo -n "Test UDP message" > /dev/udp/10.0.0.209/5005 $
Server:
Output:
From: ('10.0.0.10', 58743) received message: b'Test UDP message'
did you try to send this from outside your netwrok ?
let's say from the internet to the server behind the router?