-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp.py
56 lines (33 loc) · 1.14 KB
/
udp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import socket
localIP = "0.0.0.0"
localPort = 3004
bufferSize = 4096
msgFromServer = "Hello UDP Client"
bytesToSend = str.encode(msgFromServer)
# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
print("UDP server up and listening")
# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
#print("/n/n/n/n")
#i = 0
#while (i < len(message)):
# print(message[i])
# i += 1
res = ''.join(format(x, '02x') for x in message)
# printing result
print("The string after conversion : " + str(res))
# with open("my_file.jpg", "wb") as binary_file:
# Write bytes to file
# binary_file.write(message)
clientMsg = "Message from Client:{}".format(message)
clientIP = "Client IP Address:{}".format(address)
print(clientMsg)
print(clientIP)
# Sending a reply to client
#UDPServerSocket.sendto(bytesToSend, address)