In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to communicate between python programs, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to communicate between python programs. Let's take a look at it.
What is socket?
Socket, translated into sockets in Chinese. It is an abstraction of endpoints that communicate bidirectionally between application processes on different hosts (or different application processes on the same host) in the network. It is actually the endpoint in the various communication connections between the two networks. Its function is to complete the data transmission between applications.
Socket technology is simply through the socket, the two applications are connected, after the connection can be carried out data transmission. Socket is just a way to make a connection. The editor believes that the translation of sockets does not directly reflect the characteristics of the technology (too crude), socket can be translated into sockets, but in fact socket technology is more like "sockets" and "plugs" in software. In Chinese, socket is a kind of water pipe docking technology, which uses a socket to connect the water pipes at both ends. This technology is called socket connection, so can you understand what a socket is? How to use socket?
Different languages implement sockets in different ways, and python has packages that specifically implement sockets. Socket programming can be done only with the corresponding package of import.
After creating a socket connection, you can obtain and send data transferred between socket through the methods of recv () and send ().
Next, let's use an example code to introduce how to use socket!
Python project actual combat
Server.py
# Import socket module import socket# to create socket object serversocket = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # get the local hostname host = socket.gethostname () port = 999bind port serversocket.bind ((host, port)) # set the maximum number of connections Queue serversocket.listen (5) while True: # to establish a client connection clientsocket, addr = serversocket.accept () print ("connection address:% s"% str (addr)) msg = 'Welcome to the W3Cschool tutorial!' + "\ r\ n" clientsocket.send (msg.encode ('utf-8')) clientsocket.close ()
Client.py
# Import socket module
Import socket
# create socket object
S = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
# get the local hostname
Host = socket.gethostname ()
# set port number
Port = 9999
# Connect service, specify host and port
S.connect ((host, port))
# receive data of less than 1024 bytes
Msg = s.recv (1024)
S.close ()
Print (msg.decode ('utf-8'))
This is the end of the article on "how to communicate between python programs". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to communicate between python programs". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.