Customize your Web server using Python scripts

Source: Internet
Author: User

The title is very ambitious and far-reaching. First of all, it must be noted that this is actually an impossible task, at least not a task that can be completed by a blog post. Avoid wasting your time looking forward to the same title. I mainly want to use Apache as the blueprint and use Python scripts to implement Apache's core functions step by step. Learn more about Python network programming and all aspects related to Web service performance. Let's talk about the Code directly!
Import socket
Server = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Server. bind ('', 1234) server. listen (5) while True: client, addr = server. accept () data = client. recv (1024) client. send ("My WebServer") client. close () use the AB command to test the server: AB-n500-c500 http: // 127.0.0.1: 1234/the test result is as follows: 650) this. width = 650; "src = ".. /attachment/201301/161613741 .png" border = "0" alt = ""/> you can see that the server throughput of this simple line of code is 2000 times per second. This server can return the string "My WebServer" and cannot do anything. Therefore, the first step should at least allow the WebServer to read the corresponding file and return it to the client according to the user's request. Print data to obtain the information submitted to WebServer by the client proxy.
GET/HTTP/1.1User-Agent: curl/7.12.1 (i686-redhat-linux-gnu) libcurl/7.12.1 OpenSSL/0.9.7a zlib/1.2.1.2 libidn/0.5.6Host: 127.0.0.1: 1234 Pragma: no-cacheAccept :*/*
Here, we only care about the GET request content. After resolving the user's GET request, we GET the file content in the specified directory and then return it to the client.
Import socketimport reDocumentRoot = "/root/" server = socket. socket (socket. AF_INET, socket. SOCK_STREAM) server. bind ('', 1235) server. listen (5) while True: client, addr = server. accept () data = client. recvs (1024) m = re. match ('get /(. *) ', data) # Because AB uses http1.0, it is different from the previous example. try: # To handle possible exceptions during AB testing, index = DocumentRoot + m. group (1) html = open (index, "r") client. send (html. read () client. close () handle T: client. close ()
The result of the test using the AB command is as follows: 650) this. width = 650; "src = ".. /attachment/201301/171900362 .png" border = "0" alt = ""/> we can see that our WebServer reduces the performance by 1/4 due to the overhead of matching user requests and reading and writing files. Even so, our Web servers have a better throughput than Apache services that provide the same service. The following are the throughput of the Apache service. 650) this. width = 650; "src =" ../attachment/201301/172628416 .png" border = "0" alt = ""/>
Things are not so simple. We all noticed that our webpage cannot be 1 K. In a real production environment, the webpage size is generally more than 50 kb. Modify the index.html file and copy the 5000 line My WebServer. 650) this. width = 650; "src =" ../attachment/201301/173221532 .png" border = "0" alt = ""/>. The modified file size is 51 kb.
650) this. width = 650; "src =" ../attachment/201301/173353566 .png" border = "0" alt = ""/> test results of the Apache server. 650) this. width = 650; "src = ".. /attachment/201301/175837738 .png" border = "0" alt = ""/> while our web server cannot perform AB testing due to too many errors. You may say that because our web server is in single-process mode, it can only provide services for the next request after a complete user request is completed. But the key to the problem is that no standard http header is returned. The modified code is as follows:
Import socketimport reDocumentRoot = "/root/" server = socket. socket (socket. AF_INET, socket. SOCK_STREAM) server. bind ('', 1235) server. listen (5) while True: client, addr = server. accept () data = client. recvs (1024) m = re. match ('get /(. *) ', data) try: index = DocumentRoot + m. group (1) html = open (index, "r") index = "HTTP/1.1 200 OKContent-Type: text/html; charset = UTF-8
"+ Html. read () client. send (index) client. close () handle T: client. close ()
650) this. width = 650; "src =" ../attachment/201301/231031911 .png" border = "0" alt = ""/> the test results can still satisfy us. Our tests are conducted on local servers, so the network bandwidth is ignored. Because our server is a single process. Therefore, if one user processes slowly, the results would be unimaginable.
The code after adding multiple processes is as follows: import socketimport reimport osimport sys
DocumentRoot = "/root/" server = socket. socket (socket. AF_INET, socket. SOCK_STREAM) server. bind ('', 1234) server. listen (5) while True: try: client, addr = server. accept () failed T: sys. exit (0) if OS. fork (): client. close () else: server. close () data = client. recvs (1024) m = re. match ('get /(. *) ', data) try: index = DocumentRoot + m. group (1) html = open (index, "r") index = "HTTP/1.1 200 OKContent-Type: text/html; charset = UTF-8
"+ Html. read () client. send (index) client. close () handle T: client. close ()
You can use the AB command for testing. But Test Result: 650) this. width = 650; "src = ".. /attachment/201301/202912198 .png" border = "0" alt = ""/> the throughput has been reduced to 106.27. The reason for the failure of Python is confirmed to be the processing method of Python OS. fork (), which has no direct relationship with the system and network I/O.

This article is from the "Lincoln" blog, please be sure to keep this source http://president.blog.51cto.com/4990508/1125159

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.