Python's network programming

Source: Internet
Author: User
Tags connect socket instance method gopher socket error ftp client smtplib smtp

One, socket

Sockets are objects that provide the current portable standard for network application providers on a specific network protocol (such as TCP/IP,ICMP/IP,UDP/IP, etc.) suite pair. They allow the program to accept and make connections, such as sending and receiving data. In order to establish a communication channel, it is extremely important for each endpoint of a network communication to have a socket object.

Sockets are part of the BSD Unix system core, and they are also adopted by many other Unix-like operating systems including Linux. Many non-BSD Unix systems, such as the Ms-dos,windows,os/2,mac OS and most host environments, provide support for sockets in the form of libraries.

The three most popular types of sockets are: Stream,datagram and raw. Stream and datagram sockets can interface directly with the TCP protocol, while the raw sockets interface to the IP protocol. However, sockets are not limited to TCP/IP.

Two, Socket module

The socket module is a very simple object-based interface that provides access to the low-level BSD socket style network. Use this module to implement client and server sockets. To build a simple server with TCP and streaming sockets in Python, you need to use the socket module. Using the functions and class definitions contained in the module, you can generate programs that communicate over the network. In general, it takes six steps to establish a server connection.

The 1th step is to create the socket object. Call the socket constructor.

Socket=socket.socket (Familly,type)

To create a socket, you must use the Socket.socket () function, which is in the socket module, which has the general syntax:

s = Socket.socket (socket_family, Socket_type, protocol=0)

The following is a description of the parameter:

    • socket_family: This is Af_unix or af_inet, as I have just explained.

    • Socket_type: This is sock_stream, or for Sock_dgram.

    • protocol: This is usually not cared for, the default is 0.

The value of family can be Af_unix (UNIX domain for interprocess communication on the same machine) or af_inet (TCP and UDP for IPV4 protocol), as for the type parameter, SOCK_STREAM (stream socket), or Sock_ Dgram (data message socket), SOCK_RAW (RAW socket).

The 2nd step is to bind (assign) the socket to the specified address, socket.bind (address)

Address must be a two-element tuple, ((Host,port)), host name or IP address + port number. If the port number is being used or reserved, or if the hostname or IP address is wrong, a Socke.error exception is thrown.

3rd step, after binding, you must have the socket ready to accept the connection request.

Socket.listen (Backlog)

The backlog specifies the maximum number of connections, at least 1, after the connection request is received, the requests must be queued, and if the queue is full, the request is rejected.

4th, the server socket waits for the client to request a connection through the socket's Accept method:

Connection,address=socket.accept ()

When the Accept method is called, the socket enters the ' waiting ' (or blocked) state. When a client requests a connection, the method establishes the connection and returns the server. The Accept method returns a tuple that contains two elements, such as (connection,address). The first element (connection) is the new socket object through which the server communicates with the customer, and the second element (address) is the customer's Internet address.

The 5th step is the processing phase where the server and the customer communicate via the Send and Recv methods (transfer data). The server calls send and sends a message to the customer in string form. The Send method returns the number of characters that have been sent. The server uses the Recv method to accept information from the customer. When calling recv, you must specify an integer to control the maximum amount of data that is accepted by this call. The Recv method enters the ' Blocket ' state when the data is accepted, and finally returns a string that represents the received data. If the amount sent exceeds the allowable recv, the data is truncated. The excess data will be buffered on the receiving end. When you call recv later, the extra data is removed from the buffer.

6th step, the transfer ends, the server calls the socket's Close method to close the connection.

Establishing a simple client connection requires 4 steps.

1th step, create a socket to connect to the server Socket=socket.socket (Family,type)

2nd step, connect to Server Socket.connect ((host,port)) using the socket's Connect method

3rd, the client and server communicate via the Send and recv methods.

At the end of the 4th step, the customer closes the connection by calling the Close method of the socket.

Instance:

This is the Socket Server section:

socketssocket.socketsocket8088s.bind((host,port))s.listen(5)whiles.accept()print‘Got connection from‘, addrc.send(‘Thank you for connection‘)c.close()

This is the Socket Client section:

socketssocket.socketsocket8088s.connect((host,port))prints.recv(1024)

At run time, add the corresponding port (here is 8088) to the firewall's inbound and outbound rules.

1.2 Urllib and URLLIB2 modules

urllibAnd urllib2 is the strongest network working library in the Python standard library. The upper interface provided by these two libraries allows us to read files on the network as if they were local files. and urllib2 not urllib the upgrade version (should be a supplement), the two are not mutually replaceable.

By using urllib the urlopen function you can easily open the remote file as follows:

fromimport urlopenwebpage = urlopen(‘http://www.cnblogs.com/IPrograming/‘)txt = webpage.readline(45)print txt  # !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 

You can also access local files by adding them in front of the path file: :

fromimport urlopenwebpage = urlopen(r‘file:D:\H\sr23upd\ADD_ABBR.txt‘)txt = webpage.readline(45)print

If you can also urllib save a copy of a urlretrieve remote file directly by providing a function:

import urlretrievewebpage = urlretrieve(‘http://www.cnblogs.com/IPrograming/‘,‘C:\\temp.html‘)printtype(webpage) # <type‘tuple‘>




1.3 Other network-related modules

In addition to the sockets, Urllib, and URLLIB2, the standard library has many network-related modules, and the following list is part of it:

=========================================================== Module Description ===================================                 Enhanced version Asyncore asynchronous socket handler for ========================asynchat Asyncore               CGI basic CGI supports cookie cookie object manipulation, primarily for server operations Cookielib Client cookie supports email message support (including MIME) ftplib FTP client                     End Module Gopherlib Gopher Client blog httplib http client module                  Imaplib IMAP4 Client Module mailbox reads the format of several messages Mailcap                 Mhlib access to MH mailbox via Mailcap file access MIME configuration Nntplib NNTP Client Module poplib POP client module Robotparser support for resolving the RO of a Web server Bot file SimplexmlrpcServer A simple XML-RPC server stmpd SMTP Server module Smtplib SMTP Guest                        User-side module telnetlib Telnet Client module urlparse support parsing URL                  Xmlrpclib XML-RPC Client Support



Python Network module

You can use a list of some of the important modules in Python for network/internet programming.

Protocol Common function Port No Python Module
H TTP web pages 80 httplib, Urllib, Xmlrpclib
N NTP usenet News 119 nntplib
F TP file transfers 20 ftplib, Urllib
S MTP sending email 25 smtplib
P OP3 fetching email 110 poplib
I MAP4 fetching email 143 imaplib
T Elnet command lines 23 telnetlib
Gopher Document transfers 70 Gopherlib, Urllib

Please review the POP,IMAP agreement on the cooperation and ftp,smtp of all libraries mentioned above.





At last:

1. Set up socket

Establishing a socket object requires knowing the type of communication and the Protocol family. The type of communication indicates what protocol is used to transfer the data. Examples of agreements include IPv4, IPV6, ipx\spx, and AFP. For Internet communication, the type of communication is basically af_inet (and IPv4). The protocol family generally represents the sock_stream of TCP traffic or the SOCK_DGRAM that represents UDP traffic. So for TCP communication, the statement that establishes a socket connection is:
S=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
For UDP traffic, the statement that establishes a socket connection is:
S=socket.socket (Socket.af_inet,sock_dgram)

2. Connect socket

Connecting the socket requires a tuple, including host (hostname or IP) and port (remote port), similar to the following code:
S.connect ("www.baidu.com", 80)

3. Find the port number

The socket library uses the Getservbyname () function to query the port number, which typically requires two parameters: one is the protocol name, such as HTTP, SMTP, POP3, and so on, one is the port name, such as TCP, UDP

For example:

Import socket
S=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
Port=socket.getservbyname (' http ', ' TCP ')
The return value for port is 80. If you change to:
Port=socket.getservbyname (' SMTP ', ' TCP ')
The return value for port is 25.

4. Get information from socket

After the socket connection is established, the IP address and port number can be obtained by getsockname (), or the IP address and port number of the remote machine can be displayed via Getpeername ().
such as: in the Python shell

>>> Import Socket
>>> S=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
>>> port=socket.getservbyname (' http ', ' TCP ')
>>> s.connect (' www.baidu.com ', port)
>>> Print S.getsockname ()
(' 192.168.87.138 ', 3213)
>>> Print S.getpeername ()
(' 220.181.111.147 ', 80)

Class methods for Socket modules
Class method Description
Socket Low Level network interface (per BSD API)
Socket.socket (family, type) creates and returns a new socket object
SOCKET.GETFQDN (name) converts an IP address string separated by a dot number into a full domain name
Socket.gethostbyname (hostname) resolves a host name to an IP address string separated by a point number
SOCKET.FROMFD (FD, family, type) create a socket object from an existing file descriptor

instance method of the Socket module

Example Method description
Sock.bind ((ADRs, port) binds the socket to an address and port
Sock.accept () Returns a client socket (with client-side address information)
Sock.listen (backlog) sets the socket as a listening mode to listen for incoming connection requests from the backlog
Sock.connect ((ADRs, port)) connect the socket to the defined host and port
SOCK.RECV (buflen[, flags]) receives data from a socket, up to Buflen characters
Sock.recvfrom (buflen[, flags]) receives data from the socket, up to Buflen characters, and returns the remote host and port number of the data source
Sock.send (data[, flags]) sends data through the socket
Sock.sendto (data[, flags], addr) Send data through the socket
Sock.close () Close socket
Sock.getsockopt (LVL, optname) Gets the value of the specified socket option
Sock.setsockopt (LVL, optname, val) sets the value of the specified socket option

Example:
>>> Import Socket
>>> socket.gethostbyname (' www.baidu.com ')
' 220.181.111.147 '
>>> socket.gethostbyname (' www.126.com ')
' 123.125.50.22 '
>>> socket.getfqdn (' 123.125.50.22 ')
' 123.125.50.22 '
Getfqdn here but can't return the domain name?

5. Handling Errors
The handling of error exceptions is mainly the use of try, except statements. In the Python Network Programming Learning Note (1), gopherclient.py the following changes:

Copy CodeThe code is as follows:
#-*-coding:cp936-*-
# #modify by Little Five righteousness
Import Socket,sys
Port =70
HOST=SYS.ARGV[1]

FILENAME=SYS.ARGV[2]

Try
S=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
Except Socket.error,e:
Print "Setup socket error:%s"%e

Try
S.connect ((Host,port))
Except Socket.gaierror,e:
Print "Host or port error:%s"%e
Except Socket.error,e:
Print "Connection error:%s"%e

Try
S.sendall (filename+ "\ r \ n")
Except Socket.error,e:
Print "Data send error:%s"%e
Sys.exit (1)


While 1:
Try
BUF=S.RECV (2048)
Except Socket.error,e:
Print "Receive error:%s"%e
Sys.exit (1)
If ' does not exist ' in BUF:
Print "%s file does not exist"%filename
Else
If not Len (BUF):
Break
Sys.stdout.write (BUF)



Reference documents:

Http://www.jb51.net/article/50857.htm

Http://www.cnblogs.com/IPrograming/p/Python-socket.html

http://blog.csdn.net/alpha5/article/details/24122749

Http://www.cppblog.com/lai3d/archive/2008/02/19/42919.html

http://www.showerlee.com/archives/1051

Python's network programming

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.