Socket programming under Python

Source: Internet
Author: User
The first thing to note is that this is not a very esoteric socket programming, but it will be analyzed in one of the simplest socket programming chat room under the processing of several special exceptions, the code is as follows:

Service side:

Import sockethost = "" PORT = 8870sk = Socket.socket (socket.af_inet, socket. Sock_stream) Sk.bind ((HOST, PORT)) Sk.listen (5) while True:    print ("Server startup")    conn, addr = sk.accept ()    print ( Addr) while    True:        try:            server_recv_data = Conn.recv (1024x768)  #若客户端输入exit退出, there is no blocking, just the information received is empty            # If the client input is empty, i.e. press ENTER directly, then this will be blocked            if Len (server_recv_data) = = 0:                print ("Received as empty") Break        except Connectionreseterror as err:            print (ERR)            break        print (str (server_recv_data, encoding= "Utf-8"))        Server_resp_data = input (">>>")        conn.sendall (bytes (server_resp_data, encoding= "Utf-8"))    Conn.close () Sk.close ()

Client:

Import sockethost = "127.0.0.1" PORT = 8870sk = Socket.socket () sk.connect ((HOST, PORT)) print ("Client startup ...") while True:    INP = input (">>>")    if InP = = "Exit" or not INP: #此处需要说明的是, when you press ENTER key directly on the client, the INP is empty break    Sk.sendall (Bytes (INP, encoding= "Utf-8"))    Server_response = Sk.recv (1024x768)    print (str (server_response, encoding= "Utf-8")) Sk.close ()

Attention:

1. When the client forcibly shuts down (such as in the Pycharm in the left end of the debug window), the client will of course error, but on the Linux system, the server will not error, just think the received information is empty, that is, the server_recv_data here is empty, So this server makes a decision to exit the inner loop so that it can connect to other clients to send the connection request;

2. When the client input exit indicates to exit the current chat, where the client if Judge break exit, the key is that at this time the service side obtained the result is, Server_recv_data is empty, and the above situation;

3. The only way to make the server Server--recv_data = CONN.RECV (1024) Blocking is: The client press ENTER directly, at this time an awkward situation is the server still waiting for the client to send information (think no information arrives), the client can no longer send information, Because you have just pressed the return, although you can enter information, but could not be sent (even if you press ENTER also useless), so in order to solve this problem, we think that when the client to do this action is to exit, at this time InP is empty, so the client made if not INP: the Judgment, exit chat , the server receives the empty, and then continues to wait for other connections after the break

  • Related Article

    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.