Python network programming-Client

Source: Internet
Author: User

Python network programming-Client
The basic interface for network communication is socket, which extends the Basic I/O of the operating system to network communication. Socket can be established through the socket () function and connected through the connect () function. After obtaining the socket, you can determine the IP address and port number of the local and remote endpoints. Socket is a common interface for different protocols. It can process TCP and UDP communications. Two steps are required to establish a socket object and connect to a remote server.

Import socketprint "Creating socket" s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) # specify the communication type and protocol family print "Done! "Print" Connecting to remote host... "s. connect (" www.baidu.com ", 80) print" Done"

 

Running result 1234: Creating socketDone! Connecting to remote host... Done finds the port number through the Protocol name and port name (unix systems can find the corresponding port number in etc/services)
import socketprint "Creating socket"s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print "Done!"print "Looking up port number..."port = socket.getservbyname("http", "tcp")print portprint "done!"print "Connecting to remote host..."s.connect(("www.baidu.com", port))print "Done!!" 

 

Running result 1234567: Creating socketDone! Looking up port number... 80 done! Connecting to remote host... Done !! Obtain information from socket
Import socketprint "Creating socket" s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) print "Done! "Print" Looking up port number... "port = socket. getservbyname (" http "," tcp ") print portprint" done! "Print" Connecting to remote host... "s. connect (" www.baidu.com ", port) print" Done !! "Print" Connected from ", s. getsockname () # obtain the ip address and port number of the Local Machine (the port number is randomly allocated by the operating system and is different each time) print "Connected to", s. getpeername () # obtain the ip address and port number of the connected Machine

 

One running result 123456789 Creating socketDone! Looking up port number... 80 done! Connecting to remote host... Done !! Connected from ('2017. 168.209.129 ', 56245) Connected to ('2017. 181.112.244 ', 80) use socket communication socket objects: send (), sendto (), recv (), recvfrom () File class objects: read (), write (), readline () handling error
import socket, syshost = sys.argv[1]textport = sys.argv[2]filename = sys.argv[3]try:    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)except socket.error, e:    print "Strange error creating socket: %s" % e    sys.exit(1)try:    port = int(textport)except ValueError:    try:        port = socket.getservbyname(textport, 'tcp')    except socket.error, e:        print "Couldn't find your port: %s" % e        sys.exit(1)try:    s.connect((host, port))except socket.gaierror, e:    print "Address-related error connecting to server: %s" %e    sys.exit(1)except socket.error, e:    print "Connection error: %s" %e    sys.exit(1)try:    s.sendall("GET %s HTTP/1.0\r\n\r\n" % filename)except socket.error, e:    print "Error sending data: %s" %e    sys.exit(1)print "---------------------------------------"while 1:    try:        buf = s.recv(2048)    except socket.error, e:        print "========Error receving data: %s=======" %e        sys.exit(1)    if not len(buf):        break    sys.stdout.write(buf)print "---------------------------------------"

 

Improvement
# Coding: utf-8import socketimport sysimport timehost = sys. argv [1] textport = sys. argv [2] filename = sys. argv [3] try: s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) Doesn't socket. error, e: print "Strange error creating socket: % s" % e sys. exit (1) try: port = int (textport) failed t ValueError: print "ValueError" try: port = socket. getservbyname (textport, 'tcp ') Doesn't socket. error, e: print "Couldn't find your port: % s" % e sys. exit (1) try: s. connect (host, port) failed t socket. gaierror, e: # The host name is incorrect. print "Address-related error connecting to server: % s" % e sys. exit (1) using t socket. error, e: # print "Connection error: % s" % e sys. exit (1) print "Sleepping ......... "time. sleep (10) print "Continuing. "try: s. sendall ("GET % s HTTP/1.0 \ r \ n" % filename) handle T socket. error, e: print "Error sending data: % s" % e sys. exit (1) try: s. shutdown (1) blocks t socket. error, e: print "Error sending data (detected by shutdown): % s" % e sys. exit (1) print "-------------------------------------" while 1: try: buf = s. recv (2048) print "try" failed t socket. error, e: print "======= Error receving data: % s ======" % e sys. exit (1) if not len (buf): # accept null print "breaK" break sys. stdout. write (buf) print "---------------------------------------"

 

 

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.