current position:Home>Python Network Programming -- create a simple UPD socket to realize mutual communication between two processes

Python Network Programming -- create a simple UPD socket to realize mutual communication between two processes

2022-01-30 04:42:34 Alvin305

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities

 Socket

What is? socket

Socket Usually also known as “ Socket ”, Applications usually go through “ Socket ” Realize sending a request to the network or corresponding network request , So as to realize the mutual communication between different computers or different processes of the same computer

socket() function

stay python in , adopt socket() Function to create a socket , The syntax is as follows :

socket.socket(family,type,proto)

family: Socket family , Generally divided into AF_UNIX and AF_INET, General situation AF_INET Use more

type: Socket type , Generally divided into SOCK_STREAM and SOCK_DGRAM, If it is based on TCP The socket of the protocol uses SOCK_STREAM, If the creation is based on UDP The socket of is SOCK_DGARM

proto: The default is 0

Socket Built-in functions

socket There are a lot of built-in functions , Here are just a few functions we will use

bind(): Bind address to socket , Parameters are tuple types ,(ip,port)

sendto(): Used for sending UPD data , Send data to socket ,address In the form of (ipaddr,port) Tuple form of , Specify the address to receive information , The return value is the number of bytes sent

recvform(): Used to receive UPD data , The return value is a tuple containing data and sending address (data,address)

close(): Close socket , Be sure to close the socket after sending and receiving data

Code instance

Create two files, one for sending data sender.py And a for receiving data receiver.py. The code is as follows :

# sender.py

from socket import *

udp= socket(AF_INET,SOCK_DGRAM)
udp.bind(('',7788))# binding IP And port ,IP If it is empty, it indicates that it is local , port 7788

data = input(" Please enter what you want to send :")
upd.sendto(data.encode('utf-8'),('10.34.2.92',7789))# Data to send , And the address where the data is received , The content needs to be encoded , To prevent the occurrence of random code 

udp.close()# Close socket 
 Copy code 
#receiver.py

from socket import *

udp = socket(AF_INET,SOCK_DGRAM)
udp.bind(('',7789))

print(" Waiting to receive message ")
content ,addr = udp.recvfrom(1024)

print(" The news comes from :【%s:%s】" % (addr[0],addr[1]))
print(content.decode('utf-8'))# Need to decode , To prevent the occurrence of random code 

udp.close()# Close socket 
 Copy code 

Run code

Open two command-line windows and switch to the directory where the two files are located ,

One of the inputs :python sender.py enter , At this time, you will be prompted to enter the content to be sent , You can enter some data at will

Another input :python receiver.py enter , Then you will receive a message from sender.py Data sent .

copyright notice
author[Alvin305],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300442312676.html

Random recommended