Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize data transmission between server and client with Python socket

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how to achieve Python socket server and client data transmission related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

Socket server and client data transfer (TCP)

Server side:

Import socket# creates a socket object socket_server = socket.socket (socket.AF_INET, socket.SOCK_STREAM) host = "127.0.0.1" port = 999bind address socket_server.bind ((host, port)) # set listening socket_server.listen (5) # socket_server.accept () returns a tuple, element 1 is the client's socket object, and element 2 is the client's address (ip address) Port number) client_socket Address = socket_server.accept () # while loop is to keep the conversation going while True: # receive client request recvmsg = client_socket.recv (1024) # decode the received data strData = recvmsg.decode ("utf-8") # set exit condition if strData = = 'q': break print ("receive:% s"% strData) # input Msg = input ("send:") # send data Need to encode client_socket.send (msg.encode ("utf-8")) # close server-side socket_server.close ()

Client:

Import socket# creates a socket object client = socket.socket (socket.AF_INET, socket.SOCK_STREAM) host = "127.0.0.1" port = 999connection server client.connect ((host, port)) while True: send_msg = input ("send:") # set exit condition if send_msg = "Q": break send_msg = send_msg # send data Encoding client.send (send_msg.encode ("utf-8")) # data returned by the receiving server msg = client.recv (1024) # Decoding print ("receive:% s",% msg.decode ("utf-8")) # close the client client.close ()

Flow chart

Socket server, clients communicate with each other

Using socket to pass parameters, combine the running program with the client, start the server first, and then start the client. The basic code is as follows:

Server code

Import socketimport timeprint ("server open") # create socket mySocket = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # set IP and port # host = socket.gethostname () host = '127.0.1.1'port = 3333#bind bind this port mySocket.bind ((host, port)) # listen mySocket.listen (10) while True: # receive client connection print ("waiting for connection....") Client Address = mySocket.accept () print ("new connection") print ("IP is% s"% address [0]) print ("port is% d\ n"% address [1]) while True: # send message msg = input ("server send:") client.send (msg.encode (encoding='utf-8')) print ("send complete") Print (time.strftime ('% Y-%m-%d% HRV% MVA% S') Time.localtime (time.time () # format the timestamp into the standard format if msg = "EOF": break if msg = = "quit": client.close () mySocket.close () print ("Program ends\ n") exit () # read message msg = client .recv (1024) print ("Server receives:" Msg.decode ("utf-8") # decode the received data print ("read complete") if msg = b "EOF": break if msg = b "quit": client.close () mySocket.close () print ("Program ends\ n") exit ()

Client code:

Import socketprint ("client open") # create socket mySocket = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # set ip and port # host = socket.gethostname () host = '127.0.1.1'port = 3333 try: mySocket.connect ((host, port)) # # Connect to server print ("connect to server") except: # # connection is not successful Run the initial ip print ('connection failed') while 1: # receive message msg = mySocket.recv (1024) print ("client received:% s"% msg.decode ("utf-8")) # decode the received data print ("read complete") if msg = = b "EOF": break if msg = b "quit": mySocket.close ( ) print ("Program ends\ n") exit () # send message msg = input ("client sends:") mySocket.send (msg.encode (encoding='utf-8')) print ("send completed") if msg = = "EOF": break if msg = = "quit": mySocket.close () print ("Program ends\ n" ") exit () print (" Program ends\ n ") are all the contents of the article" how does Python socket realize data transfer between server and client? " Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report