<<python Basic Tutorials >> Learning Notes | 14th Chapter | Network programming

Source: Internet
Author: User
Tags http authentication

Python is a powerful network programming tool for two reasons:

1. There are many libraries in Python for common network protocols

2. Python's advantages in processing byte streams

This chapter mainly discusses some network modules in the Python standard library, explores the Socketserver class, and finally the twisted framework.

------

Related modules

Socket module

A basic component that is used for information channels between two programs. Sockets consist of two: Server sockets and Client sockets. After creating a server socket, let it wait for the connection so that it listens at a network address. Client sockets are responsible for: simple connection, complete transaction, disconnect. A socket is an instance of the socket class in a socket module. It requires 3 parameters for instantiation:

First parameter: Address family, default is Socket.af_inet

Second parameter: Stream (socket. SOCK_STREAM) or datagram (socket. SOCK_DGRAM)

The third parameter: the protocol used, the default is 0, the default value can be used. For a common socket, no parameters are required

After the server socket uses the Bind method, and then calls the Listen method to listen for the given address, the client socket uses connect to connect to the server. An address is a formatted Ganso (host,port). The Listen method has only one parameter, which is the length of the connection that the server has not processed (that is, the number of connections that are allowed to wait in the queue, which waits to be received before stopping the receive).

After the server socket starts listening, it can accept the client connection. This step is done using the Accept method. This method blocks (waits) until the client connects, and then returns a format of (client,address) of the Ganso, client: Clients socket, address: one of the previously interpreted addresses. The server can handle the client to its satisfaction, and then call another accept method to start waiting for the next connection. This process is usually implemented in an infinite loop. Sockets have two methods: Send and recv, which are used to transfer data.

The simplest of client/server

Server-side Programs

Import sockets = Socket.socket () host = Socket.gethostname () port = 1234s.listen (5) while True:    c.addr =s.accept ()    print ' Got connect from ', addr    c.send (' Thank to connecting ')    C.close ()
Client Programs
Import sockets = Socket.socket () host = Socket.gethostname () port = 1234s.connect ((host,port)) print S.RECV (1024)

------

Urllib and URLLIB2 Modules

In the network library, the most powerful are urllib and URLLIB2. The two modules function the same, but the urllib2 is a little better. If you use just a simple download, urllib is enough. URLLIB2 is the best choice if you need to use HTTP authentication or cookies, or if you want to write extensions for your own protocols.

1. Open a remote file

#可以使用只读模式, using urlopen from Urllib, not open or file

>>> from urllib import urlopen>>> webpage = urlopen (' http://www.python.org ')
#urlopen返回的类文件对象支持close, Read,readline,readlines, also supports iterations.

If you want to extract the URL of the "about" link in a Python page, you can use a regular expression to implement it.

>>> Import re>>> Text = Webpage.read () >>> m = Re.search (' <a href= ' ([^ "]+). *" >about</a> ', Text,re. IGNORECASE) >>> m.group (1) '/about/'
2. Get Remote Files

If you want Urllib to download a file for you and store a copy of the file locally, you can use Urlretrieve. It returns a meta-ancestor (filename,headers) instead of a class file object, filename is the name of the local file, headers contains some information about the remote file, and if you want to specify a file name for the downloaded copy, Can be given in the 2nd parameter of the Urlretrieve function.

>>> urlretrieve (' http://www.python.org ', ' c:\\python_webpage.html ')

#该语句获取Python的主页并把它存储在文件python_webpage. html, if not specified, the file is placed in a temporary location, opened with the Open function, and deleted to save disk space after the operation is completed. To clean up temporary files, you can use the Urlcleanup function, which is responsible for cleaning up the work.

------

Other modules


------

Socketserver

If you want to implement an application that goes beyond the basics, use Socketserver, which is the basis for many server frameworks, including Basehttpserver,simplehttpserver,cgihttpserver, Simplexmlrpcserver and Docxmlrpcserver.

The Socketserver contains 4 basic classes:

TCPServer for TCP sockets stream

Udpserver for UDP datagram sockets

For Unxistreamserver

For Unixdatagramserver

The first class used primarily.

Code Listing: A small server based on Socketserver

From Socketserver import TCPServer, Streamrequesthandlerclass Handler (Streamrequesthandler):    def handle (self):        addr = self.request.getpeername ()        print ' Got connection from ', addr        self.wfile.write (' Thank Connecting ') Server = TCPServer ((' ', 1234), Handler) Server.serve_forever ()
------

Multiple connections:

3 ways to process multiple connections at a time: fork (forking), Thread (threading), and asynchronous I/O (asynchronous I/O)

Code listing: Servers using fork Technology

From Socketserver import TCPServer, forkingmixin, Streamrequesthandlerclass Server (forkingmixin,tcpserver): Passclass Handler (Streamrequesthandler):    def handle (self):        addr = Self.request.getpeername ()        print ' Got Connection from ', addr        self.wfile.write (' Thank-for-connecting ')        Server = Server ((' ', 1234 '), Handler) Server.serve_forever ()
Code Listing: Server with threading

From Socketserver import TCPServer, ThreadingMixIn, Streamrequesthandlerclass Server (ThreadingMixIn, TCPServer): Passclass Handler (Streamrequesthandler):    def handle (self):        addr = Self.request.getpeername ()        print ' Got Connection from ', addr        self.wfile.write (' Thank to connecting ') Server = Server ((' ', 1234), Handler) Server.serve _forever ()
------

Twisted

The twisted from the Twisted Matrix Lab is an event-driven Python network framework that was originally developed for online games and is now used by all types of Web software. Twisted can work well with several common GUI toolkits (TK,GTK,QT and wxwidgets). Twisted is a very rich framework and supports Web servers, clients, Ssh2,smtp,pop3,imap4,aim,icq,irc,msn,dns and so on.

------

Installing twisted

Download URL: https://twistedmatrix.com/trac/can choose your own operating system, Python version to install.

Under the Windows platform, you can either install the. msi or. exe file or install the python setup.py installation with Distutils

Other platform system, can choose Rpm,apt,portage, etc., to install

------

Writing a twisted server

Code Listing: Simple Server with Twisted

From twisted.internet import reactorfrom twisted.internet.protocol Import protocol, Factoryclass Simplelogger (protocol ):    def connectionmade (self):        print ' Got connection from ', Self.transfort.client    def connectionlost (self, Reason):        print self.transport.client, ' disconnected '    def datareceived (self, data):        Print DataFactory = Factory () factory.protocal = Simplelogger
Code Listing: Logging Server with Linereceiver protocol improvements

From twisted.internet import reactorfrom twisted.internet.protocol import factoryfrom twisted.protocols.basic Import Linereceiverclass Simplelogger (linereceiver):    def connectionmade (self):        print ' Got connection from ', Self.transport.client    def connectionlost (self, Reason):        print self.transport.client, ' disconnected '    def linereceived (self, Reason):        print linefactory = Factory () Factory.protocol = Simpleloggerreactor.listentcp ( 1234, Factory) Reactor.run ()
------



<<python Basic Tutorials >> Learning Notes | 14th Chapter | 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.