Python socket programming and Pythonsocket Programming

Source: Internet
Author: User

Python socket programming and Pythonsocket Programming

The first thing to note is that it does not record very profound socket programming, but it will analyze the handling of several special exceptions in the simplest socket programming chat room. The Code is as follows:

Server:

1 import socket 2 3 HOST = "" 4 PORT = 8870 5 sk = socket. socket (socket. AF_INET, socket. SOCK_STREAM) 6 sk. bind (HOST, PORT) 7 sk. listen (5) 8 while True: 9 print ("server startup") 10 conn, addr = sk. accept () 11 print (addr) 12 while True: 13 try: 14 server_recv_data = conn. recv (1024) # If the client enters exit to exit, there is no blocking here, but the received information is blank. 15 # If the client input is blank, press the Enter key, 16 if len (server_recv_data) = 0: 17 print ("Blank received") 18 break19 failed t ConnectionResetError as err: 20 print (err) will be blocked here) 21 break22 print (str (server_recv_data, encoding = "UTF-8") 23 server_resp_data = input (">>>") 24 conn. sendall (bytes (server_resp_data, encoding = "UTF-8") 25 conn. close () 26 sk. close ()

Client:

1 import socket 2 3 HOST = "127.0.0.1" 4 PORT = 8870 5 sk = socket. socket () 6 sk. connect (HOST, PORT) 7 print ("Start the client... ") 8 9 while True: 10 indium = input (" >>> ") 11 if indium =" exit "or not indium: # Here, it must be noted that, when press enter directly on the client, the enter key is blank. 12 break13 sk. sendall (bytes (indium, encoding = "UTF-8") 14 server_response = sk. recv (1024) 15 print (str (server_response, encoding = "UTF-8") 16 sk. close ()
Note:

1. when the client is forcibly disabled (for example, when you press the stop button on the left side of the debugging window in pycharm), the client certainly reports an error, but on Linux, the server does not report an error, only the received information is null, that is, server_recv_data is null. Therefore, the server makes a judgment to exit the inner loop so that it can connect to other clients to send connection requests;

2. When the client enters exit to exit the current chat, if the client determines that break exits, the key is that the server obtains the result that server_recv_data is empty, which is the same as the above;

3. unique server -- recv_data = conn. recv (1024) is blocked: the client directly press the Enter key. In this case, the server is still waiting for the client to send the message (it is considered that no information has arrived), and the client cannot send the message again, because you have already press enter, you can enter the information again, but you cannot send it (even if you press enter again), so to solve this problem, by default, we think that when the client performs this action, it indicates that it wants to exit. In this case, the "if not P:" statement is made on the client, and the "Exit chat" function is null, continue waiting for other connections after break

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.