In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "the realization of python socket socket communication". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "python socket socket communication in the implementation" can help you solve your doubts.
I. preliminary understanding
Socket has been translated into sockets, although some people criticize it, but I think it is quite appropriate. Its function is to provide low-level network services, the most commonly used is to transfer data according to IP.
The so-called transmission, there must be two "ends", first of all to be a server
Import sockets = socket.socket () host = socket.gethostname () # Native address port = 12345 # Port s.bind ((host,port)) # bind Port s.listen (3) # start listening Support up to three links while True: C, addr = s.accept () # waiting for connection print ("Linked @ Addr", addr) break# to send the command while True: data= input ("input data:") c.send (data.encode ("utf8")) if data== "exit": c.close () break
It should be noted that what send needs to send is binary code, so it is encoded and decoded by encode and decode. Finally, exit if you enter exit.
And then write a client.
Import sockets = socket.socket () host = socket.gethostname () port = 12345s.connect ((host,port)) while True: data = s.recv (1024). Decode ("utf8") if data disabled bundles: print ("receive data:", data) # close the port when exit is received, exit the loop if data [: 4] = "exit": s.close () break
After running, the input and output of the server and the client are respectively
# Linked @ Addr ('192.168.1.113clients, 9953) input data:hello world11input data:who are you11input data:can u speak chinese?20input data:exit# client receive data:hello worldreceive data:who are youreceive data:can u speak chinese?receive data:exit II, socket object
In the above example, a socket object is created through socket.socket, and its complete constructor is
Socket.socket (family=AF_INET, type=SOCK_STREAM,proto=0,fileno=None)
Among them, family represents the address family of sockets, which mainly includes three types
Address family AF_INETAF_INET6AF_UNIX protocol source IPv4IPv6UNIX
When creating a serial port, you can select multiple address families at the same time by or operation.
Type is a socket type, and there are two more commonly used ones:
SOCK_STREAM, which is a streaming socket, is characterized by the same order of transmission and reception and security.
SOCK_DGRAM, a Datagram format socket that is fast, out of order, and may be lost
Proto is the protocol number, usually 0. When the protocol family is AF_CAN, the protocol should be CAN_RAW, CAN_BCM, CAN_ISOTP or CAN_J1939.
Fileno represents a file that creates a socket.
Although the constructor does not declare client-side and server-side parameters, functionally, the members they can call should not be exactly the same.
Among them, the three methods of bind,listen,accept are proprietary methods on the server side, and their functions are
Bind (address): bind it to an address, where the address address is usually a tuple, including IP and port number
Listen (N): start a server to accept connections. N is the maximum number of connections, not less than 0.
Accept (): accepts a connection with no parameters. The return value is (conn, address) tuple. Conn is a new socket object that is used to send and receive data.
Accordingly, the client also has two proprietary methods:
Connect (address): connect to an address.
Connect_ex (address): compared to connect, an error code is returned when an error occurs without reporting an error.
Then there are methods that can be used by both the client and the server, the most critical of which are sending send and receiving recv.
Among them, functions related to sending are:
Send (bytes): where bytes is the sent byte and returns the sent byte (sometimes it may not be finished).
Sendall (bytes): compared to send, bytes continues to be sent until all data has been sent or an error has been reported.
Sendfile (file,offset=0,count=None): send a file under Unix, which is the same as send under Windows, which is equivalent to being unavailable.
Sendto (bytes,addresss): specifies the address to send data.
There are two sets of receive correlation functions available in Windows, where bufsize represents the maximum number of bytes of received data.
Return data return data + receiver address does not write buffer recv (bufsize) recvfrom (bufsize) write buffer bufrecv_into (buf,bufsize) recvfrom_into (buf,bufsize)
Get-set is a function name that appears in many modules. The former represents getting certain parameters, the latter represents setting certain parameters, and generally the input of the latter is the output of the former.
There are several ways to abort or close a socket:
Close () closes the file descriptor of the socket
Detach () closes the socket object but does not close the file descriptor
Shutdown (how) can partially close the connection of the socket, where the how is:
SHUT_RD: reception is no longer allowed later
SHUT_WR: sending is no longer allowed later
SHUT_RDWR: subsequent sending and receiving are not allowed
Read this, the "python socket socket communication in the implementation" article has been introduced, want to master the knowledge of this article still need to practice and use to understand, if you want to know more about the article, 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.