Network Programming -day1

Demand for network communication: target ip and port

Use socket library

udp transmitter

 1, we need to create a container

2, the setting information needs to be transmitted and ip, port

3, the socket is closed

Socket Import 
UDP_SOCKET = socket.socket (socket.AF_INET , socket.SOCK_DGRAM) # create a container, transmitted using a protocol udp
udp_socket.sendto ( b'hahaha ' , ( "192.168.19.130" , 8080)), and the setting information transmitting # ip, port
udp_socket.close ()

Receiver udp

1, need to bind a port

2, the data reception standby

3, the received display data

4, the socket is closed

 

socket Import 
UDP_SOCKET = socket.socket (socket.AF_INET , socket.SOCK_DGRAM)
# create the socket
local_add = ( "" , 7788)
# port received
udp_socket.bind (local_add)
# bind port
recv_data = udp_socket.recvfrom ( 1024)
# waiting to receive data, 1024 represents the maximum number of bytes received
Print (recv_data [ 0] .decode ( 'GBK'))
# printing the received data, because there are two tuples, the received information, and transmitting party ip and port
udp_socket.close ()
# close the socket

 




Guess you like

Origin www.cnblogs.com/Alom/p/11520160.html