In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "Python based on socket how to achieve TCP client and server", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Python based on socket how to achieve TCP client and server" bar!
First, based on the socket implementation of TCP client import socket # establish socket object # parameter 1 indicates the IP address type (AF_INET is IPV4,AF_INET6 is IPV6) Parameter 2 indicates the type of connection (SOCK_STREAM represents TCP form, SOCK_DGRAM represents UDP form) client_socket=socket.socket (socket.AF_INET,socket.SOCK_STREAM) # represents (ipv4,TCP) # connection server (fill in the target ip address and port number in the tuple) client_socket.connect (('127.0.0.1 port number)) # prepare data, which needs to be converted to binary data Encode () fills in the local string encoding format. Mac and linux fill in utf-8data='hello'.encode ('gbk') # send data to the server client_socket.send (data) # receive data, you must specify the size of the received data, unit bytes, maximum 4096, that is, 4krecv_data=client_socket.recv (1024) # received data to be decoded by decode () Fill in the code recv_data=recv_data.decode ('gbk') print (recv_data) # close the connection client_socket.close () 2. Establish the socket object server_socket=socket.socket (socket.AF_INET,socket.SOCK_STREAM) based on the import socket# of the TCP server implemented by socket # if the server is one-time, there will be an error if the server is restarted immediately after the server ends The reason is that the address and port are not released # OSError: [Errno 48] Address already in use# if you want to release immediately, set the socket option server_socket.setsockopt (socket.SOL_SOCKET,socket.SO_REUSEADDR,True) # bind IP and port. If no IP is given when binding IP, the default is to bind local IPserver_socket.bind ((', 7777)) # set listener (maximum number of listeners) After setting up, the server will enter the passive mode and cannot connect to the client actively, so it can only passively wait for the client to connect server_socket.listen (128) # wait for the client to connect. The function returns the client's Socket object and address information client_socket Ip_port=server_socket.accept () print (f 'client {ip_port [0]} connected successfully using port {ip_port [1]}...') # receive client data data=client_socket.recv (1024) # View the data length sent by client if len (data)! = 0: data=data.decode ('gbk') print (f' client {ip_port [0]} use port {ip_port [ 1]} send is data is {data}') else: print (f 'client {ip_port [0]} uses port {ip_port [1]} closed connection') # send data to client data=' Hello. Encode ('gbk') client_socket.send (data) # close client client_socket.close () # close server server_socket.close () Thank you for reading The above is the content of "how Python implements TCP client and server based on socket". After the study of this article, I believe you have a deeper understanding of how Python implements TCP client and server based on socket, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.