Python Socket Foundation (Server)-Foundations of Python sockets

Source: Internet
Author: User

1Python Socket Base Server-Foundations of Python sockets2 3A server that provides a TCP link service through a Python socket module can be divided into 4a step,41, set up the socket object5s =Socket.socket (socket.af_inet, socket. SOCK_STREAM)62, set the socket option (optional)7S.setsockopt (socket. Sol_socket, SOCKET. SO_REUSEADDR, 1)8 after a server process terminates, the operating system typically retains a few ports, so that before the timeout9 the port is first occupied and cannot be used by other programs. If you set SO_REUSEADDR to True,Ten The operating system releases its port immediately after the socket is closed. One3, bind to a port, or a network card A S.bind ((host, port)) -The host parameter can be empty"', which means binding to all addresses. - A use case is that a PC has multiple network cards, a network card linked to the public Internet, the Another network card is linked to the internal network. If you want the service to be visible only to the internal network, bind () - method binds the service to the IP address of the internal network. This allows access to the public Internet through the - The services used are not visible.  -4, listen for link requests +S.listen (5) - This method notifies the operating system that it is ready to accept a link request. The method receives an int type parameter, meaning to indicate + when the server processes the link, the number of waiting links is allowed in the queue. A  at respond to connection requests, -      while1: -Clientsocket, clientaddr =s.accept () -         Print("%s is connecting"%clientsocket.getpeername ()) - the Accept () method blocks the program and returns when a client connects -  in exception handling, - any exception that is not caught will be terminated by the program, but this situation is very bad for the server. to because the exception will close the entire server and stop responding to the request, you have to restart the service side to reply to the service.  + a robust service-side program that captures any errors and exceptions and handles them appropriately to ensure that the service is not terminated. -     ImportTraceback the      while1: *         Try: $Clientsocket, clientaddr =s.accept ()Panax Notoginseng         exceptKeyboardinterrupt as E:#Ctrl-c can terminate the program -             Raisee the         except: +Traceback.print_exc ()#catch so other exceptions and print A             Continue                               #Next loop, rather than terminating the program the         Try: +             Print("%s is connecting"%clientsocket.getpeername ()) -         except(keyboardinterrupt,systemexit):#ctrl-c and Sys.exit () $             Raise $         except: -Traceback.print_exc ()#Printing Other exceptions -      the         Try: -Clientsocket.close ()#after entering the second try code block,Wuyi                                                    #The close () method should be called regardless of whether it has an XOR effect. the         exceptKeyboardinterrupt:#Second, three try code blocks, which can be combined into a -                                                    #The Try ... finally structure guarantees the call of close (). Wu             Raise -         except: AboutTraceback.print_exc ()

Python Socket Foundation (Server)-Foundations of Python sockets

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.