Python learning Chapter 2-Network Programming

Source: Internet
Author: User
Tags http authentication

1. socket: A socket is an instance of the socket class in the socket module. It needs three parameters for instantiation: the first parameter is the address family (the default is socket. AF_INET); the second parameter is stream (socket. SOCK_STREAM, default value) or datagram (socket. SOCK_DGRAM) socket; the third parameter is the protocol used (the default value is 0. Use the default value ). For a common socket, no parameters are required. The server socket uses the bind method, and then calls the listen method to listen to the given address. The client socket uses the connect method to connect to the server. The address used in the connect method is the same as the address in the bind method (on the server side, many functions can be implemented, such as using the function socket. gethostname to get the current host name ). The listen method has only one parameter, that is, the length of the server's unprocessed connections (that is, the number of connections that can be queued for waiting. These connections are waiting for receiving before they are stopped ). After the server socket starts listening, it can accept the client connection. This step is completed using the accept method. This method will be blocked until the client connects, and then the method will return a tuples in the format of (client, address). client is a client socket, and address is the listening address. The socket has two methods: send and recv (used for receiving) for data transmission. You can use string parameters to call send to send data, and use a required maximum number of bytes as the parameter to call recv to receive data. If you cannot determine which number to use is better, 1024 is a good choice. The following is a simple server/client example. The example is very simple and similar to network programming in java:

2. urllib and urllib2 modules: These two modules can access files through the network, just as those files exist on your computer. Urllib is sufficient if simple download is used. urllib2 is a good choice if you need to use HTTP authentication or cookies or write extensions for your own protocol.
Open a remote file:
From urllib import urlopen
Webpage = urlopen ('HTTP: // www.python.org ')
If online, the variable webpage should now contain a class file object that is linked to the http://www.python.org web page. The class object returned by urlopen supports the close, read, readline, and readlines methods. Of course, iteration is also supported.
Obtain remote files: The urlopen function provides a class file object that can read data from. If you want urllib to download the file for you and store a copy of the file in the local file, you can use urlretrieve. Urlretrieve returns a tuple (filename, headers) instead of a Class Object. filename is the name of a local file (automatically created by urllib). headers contains information about remote files. If you want to specify a file name for the Downloaded copy, you can give it in the second parameter of the urlretrieve function. Urlretrieve ('HTTP: // www.python.org ', 'c: \ python_webpage.html') obtains the python homepage and stores it in the file c: \ python_webpage.html. You can use the urlcleanup function to clear temporary files without providing parameters.
Two common modules are described above. Other network-related modules include:

3. socketServer: To write a server that uses the socketserver framework, most of the code will be in a request processing program. Whenever the server receives a request, it will instantiate a request processing program, and its various processing methods will be called when processing the request. The method to call depends on the specific server and the processing program class used. This way, you can subclass them so that the server can call a custom processing assembly. The basic BaseRequestHandler class puts all the operations into a handle method of the processor. This method will be called by the server. Then this method will access the client socket in the property self. request. If the stream is used, you can use the StreamRequestHandler class to create two new attributes, self. rfile (for reading) and self. wfile (for writing ). Then you can use these file objects to communicate with clients. The following example shows a small server based on SocketServer:

4. Use SocketServer for forks and thread processing (windows does not support forks ):
Servers using the forking technology:

Servers processed by threads:

Author: uohzoaix

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.