Receiving UDP message in Python - python

I am trying to get a simple Python script to receive UDP messages, and I cannot get it to work. The following code is based on other suggestions on this forum.
import socket
#UDP_IP_REC = "192.168.10.1"
UDP_PORT_REC = 5005
sock_rec = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock_rec.bind(('', UDP_PORT_REC))
while True:
data, addr = sock_rec.recvfrom(4096) # buffer size is 1024 bytes
print("received message: %s" % data)
I have the following code sending the data that I would like to receive:
import socket
Forces = [2.1,2.2,2.3,0.1,0.2,0.3]
UDP_IP = "192.168.10.1"
UDP_PORT = 5005
MESSAGE = b'Forces: %.2f, %.2f, %.2f, %.2f, %.2f, %.2f' % (Forces[0], Forces[1], Forces[2], Forces[3], Forces[4], Forces[5])
print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
while True:
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
wait = input("Press Enter to continue.")
I am sending from a virtual machine to my native OS, and I can use wireshark to see the message at the receiving side. However, the recvfrom() function is not receiving anything. I am ruling out firewall issues since I can see the packets in wireshark.
Any ideas as to something seemingly so simple does not work?

Related

Receiving UDP packet without IP address

I'm trying to implement simple DHCP client. The problem is receiving UDP broadcast packets on network interface that hasn't assigned ip address yet.
I think I can simplify my issue to following scripts that I execute in docker containers working in the same bridge network.
sender.py:
import socket
UDP_IP = "255.255.255.255"
UDP_PORT = 5005
MESSAGE = b"Hello, World!"
print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.setsockopt(socket.SOL_SOCKET, 25, str(f"eth1" + '\0').encode('utf-8'))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
receiver.py:
import socket
UDP_IP = ""
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
sock.setsockopt(socket.SOL_SOCKET, 25, str(f"eth1" + '\0').encode('utf-8'))
while True:
data, addr = sock.recvfrom(1024)
print("received message: %s" % data)
If i execute upper scripts when ip address is statically assigned to eth1- interface (172.1.2.2) i receive packet. But after deleting ip address:
ip addr del 172.1.2.2/24 dev eth1
I can't receive anything.
Of course i can observe sent packet on eth1 on receiver side by tcpdump.
How can i receive UDP broadcast packet, on interface that doesn't have assigned ip address ?

Sending CAN frame via UDP in Python

I made UDP socket connection between two Linux machines and can send for example b"Hello, World!" easily. But now I need to send the below CAN frame
from can import Message
send_msg = Message(data=[1, 2, 3, 4, 5])
So if I print send_msg it shows:
Timestamp: 0.000000 ID: 00000000 X DLC: 5 01 02 03 04 05
I want to get this printed on the receiving end. The sending and receiving end codes I am using are below:
Sending:
import socket
UDP_IP = "10.140.189.249"
UDP_PORT = 5005
from can import Message
send_msg = Message(data=[1, 2, 3, 4, 5])
print(send_msg)
MESSAGE = send_msg
print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
Here I know MESSAGE = send_msg is wrong.
Receiving
import socket
UDP_IP = "0.0.0.0"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
rec_msg, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print("received message: %s" % rec_msg)
Please advise
As the physical layer of an UDP connection and a CAN connection are substantially different, you cannot send the CAN frames over UDP. What is of course possible is to send the payload of the CAN frame and assemble the CAN message on the receiving side: I.e. on the sending side:
sock.sendto(b“12345“, (UDP_IP, UDP_PORT))
And on the receiving side:
msg = Message(data=bytearray(recv_msg))
Most likely you do not only want to transfer the data of the CAN frame, but also the ids and other fields.
Another possibility would be to pickle the Message object on the sending side and unpickle it on the receiving side using pickle.dumps and pickle.loads
All features of the CAN bus like arbitration, error-frames etc. cannot be mimicked on a UDP connection.

UDP listener [Errno 99]

In the image, the specs of the UDP package I aim to capture is shown through WireShark. The UDP packet comes from a different client attached to my router. However, when trying to capture this using socket through python, I keep on getting OSError: [Errno 99] Cannot assign requested address.
Currently I am doing following:
import sys
import socket
##Capture UDP packets
UDP_IP = "192.168.13.13"
UDP_PORT = 1667
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the port
server_address = (UDP_IP, UDP_PORT)
print(sys.stderr, 'starting up on %s port %s' % server_address)
sock.bind(server_address)
while True:
print (sys.stderr, '\nwaiting to receive message')
data, address = sock.recvfrom(4096)
print (sys.stderr, 'received %s bytes from %s' % (len(data), address))
print (sys.stderr, data)
Is this the correct address to use?

proxy for udp packet allows packets to pass through, but does not change their source ip address

I'm trying to route my outbound udp packets through an external proxy server (not expecting traffic to return atm). The traffic makes it to the destination server, however the source ip address in the packets is my client's ip address rather than the external proxy's.
What am I doing wrong?
from scapy.all import *
import socks, socket
sent = False
def intercept(pkt):
if pkt.haslayer(UDP):
s = socks.socksocket(socket.AF_INET, socket.SOCK_DGRAM)
s.setproxy(socks.PROXY_TYPE_SOCKS5, "proxy-server-ip", 19012, True)
dst_addr = pkt[IP].dst
global sent
if not sent:
dst_port = pkt[UDP].dport
address = (dst_addr, dst_port)
s.sendto(bytes("test", 'utf-8'), address)
sent = True
def main():
sniff(iface='ens3', prn=intercept)
if __name__ == '__main__':
main()
The way I'm checking the udp packets are with a small client/server configuration.
Client:
import socket
import sys
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('server-ip', 10000)
message = 'This is the message. It will be repeated.'
try:
# Send data
print >>sys.stderr, 'sending "%s"' % message
sent = sock.sendto(message, server_address)
# Receive response
print >>sys.stderr, 'waiting to receive'
data, server = sock.recvfrom(4096)
print >>sys.stderr, 'received "%s"' % data
finally:
print >>sys.stderr, 'closing socket'
sock.close()
Server:
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the port
server_address = ('0.0.0.0', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
while True:
print >>sys.stderr, '\nwaiting to receive message'
data, address = sock.recvfrom(4096)
print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
print >>sys.stderr, data
if data:
sent = sock.sendto(data, address)
print >>sys.stderr, 'sent %s bytes back to %s' % (sent, address)

Cannot send and receive udp message in same program

I am able to send and receive UDP messages in separate programs, but I'm not able to do the same task in one program.
import socket
UDP_IP = "192.168.1.178"
UDP_PORT = 8888
msg = 'test'
print "UDP target IP: ", UDP_IP
print "UDP target PORT: ", UDP_PORT
print "Message: ", msg
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(msg, (UDP_IP, UDP_PORT))
UDP_IP2 = "192.168.1.198"
sock.bind((UDP_IP2, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
With this program, I am able to send UDP messages, however, I am not able to receive any messages from the other machine.
What am I doing wrong?
Thanks in advance,
Mikkel
In your example you try to bind socket addr after sending, what's wrong.
Address can be bound to socket only before any data transfer.
If there is no explicit bind OS sets any free (unused) port number in range [1024, 65535] on first .send()/.recv() call.
Next, socket can be bound only to single IP (except special case '0.0.0.0' which means "all host's interfaces").

Categories

Resources