Answers for "python udp connection"

2

python UDP server

####### UDP Server #######

from socket import socket,AF_INET , SOCK_DGRAM

sobj = socket(AF_INET,SOCK_DGRAM)


ip = '127.0.0.1'
port = 20000

sobj.bind((ip,port))

while True:
    msg = sobj.recvfrom(2048)
    print(msg)
    
####### UDP Client #######

from socket import socket,AF_INET , SOCK_DGRAM

sobj = socket(AF_INET,SOCK_DGRAM)


ip = '127.0.0.1'
port = 20000

msg = "Hello World"
sobj.sendto(msg.encode(),(ip,port))
Posted by: Guest on August-25-2021
2

python UDP send

import socket

SOURCE_IP = "192.168.1.123"
SOURCE_PORT = 5678
DESTINATION_IP = "192.168.1.124
DESTINATION_PORT = 5679

# create the socket
udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
udp_socket.bind((SOURCE_IP,SOURCE_PORT))

# prepare the payload
payload_hex_string = "68656c6c6f5f776f726c64" #hello_world
payload = bytes.fromhex(payload_hex_string)

#send out the packet
udp_socket.sendto(payload,(DESTINATION_IP,DESTINATION_PORT))
Posted by: Guest on May-26-2021

Python Answers by Framework

Browse Popular Code Answers by Language