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?
Related
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 ?
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?
How to capture loop back traffic in Python, with Wireshark i am able to observe the communication as below:
I want to be the "Man In the middle" in this scenario. As i am trying to make the simulator of a Trace32 debugger in my system.
Edit 10-Aug-2020:
I have tried below code, and receiving error, it seems we can not reuse the same port:
OSError: [WinError 10048] Only one usage of each socket address
(protocol/network address/port) is normally permitted
import socket
UDP_IP = "127.0.0.1"
DEBUG_PORT = 20000
MASTERPORT = 53180
MESSAGE = b"TRACE32 C"
mastersock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
debugsock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
debugsock.bind((UDP_IP, DEBUG_PORT))
while True:
data, addr = debugsock.recvfrom(1024) # buffer size is 1024 bytes
print("Listen on Debugger port: %s" % data)
if (data):
print ("Send back response to Master")
mastersock.sendto(MESSAGE, (UDP_IP, MASTERPORT))
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)
So, I have this code here. This sender script give's me the output properly.
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 0
MESSAGE = "Hi, can you listen to this?"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
I tried to use this script on another host and try to establish a communication between the two. (Both the systems are on the same network ex. 00.000.00.xxx , only the xxx part varies)
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 0
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
Here is the output
Traceback (most recent call last):
File "C:/Users/bshivaku/Desktop/SEnd_Udp_packets.py", line 9, in <module>
sock.bind((UDP_IP, UDP_PORT))
File "C:\Python27\Lib\socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10049] The requested address is not valid in its context
I used the ip address of the receiver on the sender script and sender ip address on receiver
I am sure I made a mistake with the UDP_PORT so i used PORT= 0 and tried. How to request for port number? How do I establish the connection? If not the port, where am I going wrong?
When sending a message use a specific port, for example UDP_PORT=8765, otherwise if UDP_PORT is set to 0 then the system will chose a random port for you.
Use the ip address of the receiver host in the sender script and bind to any interface on the receiver script.
On the receiving side use the same UDP port number configured in the sender script.
receiver:
import socket
UDP_IP = "0.0.0.0"
UDP_PORT = 8543
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
sender:
import socket
UDP_IP = "<ip_address_of_receiver>"
UDP_PORT = 8543
MESSAGE = "Hi, can you listen to this?"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
Aside the UDP addresses setting there are not any problems on python side.
About the error:
[Errno 10049] The requested address is not valid in its context
This normally stems from an attempt to bind to an address that is not valid for the local computer: so it seems that the loopback address 127.0.0.1 it is not configured on your machine.
Investigate on the sys admin side, for example check if the IPv4 network stack is enabled on your machine.