Python network programming, obtaining port numbers through service names and session types (TCP,UDP), simple exception handling

Source: Internet
Author: User

As a PHP programmer, but also interested in the network, PHP compared to the egg pain, so I took some time to see Python

Before Learning Python Foundation for work reasons, intermittent to see a foundation, almost can write code

Recently, I was looking at the basics of Python network programming, and was prepared to learn by practicing, copying an example of a book.

Code that runs under the CLI

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #^ Set Encoding: Noon in code will cause compilation errors4 5 #Introducing the Socket,sys module6 ImportSocket,sys7 8 #receive parameters passed by the CLI (service name)9Ser_name = sys.argv[1]Ten  One Print "Creating Socket ..." A #Create socket -s =Socket.socket (Socket.af_inet,socket. SOCK_STREAM) - Print "Dine." the  - Print "looking up port number ..." -  - #Exception capture: The service name passed by the CLI may be incorrect for this exception handling + Try: -     #get the port number by service name and reply type +Port = Socket.getservbyname (Ser_name,'TCP') A exceptSocket.error as E: at     #Exception -     Printe -Sys.exit (1) -  - Print "Done ." -  in Print "Connect to remote host%d ..."%Port -  to #catch Exception: The server may not have the corresponding service turned on, or the network problem causes the connection failure to occur unexpectedly + Try: -     #connecting to the service side theS.connect (("www.baidu.com", port)) * exceptSocket.error as E: $     #ExceptionPanax Notoginseng     Print "Strange Error creating socket:%s"%e -Sys.exit (1)     the  + Print "Done ." A  the #information that can be obtained when a connection is successfully established + #client-initiated connection IP and port - Print "Connect from", S.getsockname () $ #IP acquisition ports on the server side $ Print "Connect to", S.getpeername ()

Python network programming, obtaining port numbers through service names and session types (TCP,UDP), simple exception handling

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.