In fact, we can use a pure Python language to implement the WEB. After implementation, we still need to test this function. Here, we will use some methods to test Python WEB implementation. I hope you can take this as a learning reference object.
- How to compile Python code for capturing Web Images
- Explanation of the specific application method of the Python submission form
- Python file type
- Python yield usage
- Analysis on the basic operation steps for Python Access Database Operations
Python WEB testing environment:
Server Configuration: 4 x Intel (R) Xeon (R) CPU E5405 @ 2.00 GHz, 4G memory, operating system: CentOS 5.3 x86_64
Nginx frontend + 4 tornado (0.2) web process
Tornado: http://www.tornadoweb.org (walled)
Python WEB testing:
For http get requests, the server directly returns "hello world"
Python implements WEB code and nginx Configuration:
- main.py:
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- """web main"""
- from tornado.httpserver import HTTPServer
- from tornado.ioloop import IOLoop
- from tornado.web import RequestHandler, Application, authenticated
- #from rockps.auth import AuthHandler
- class MainHandler(RequestHandler):
- def get(self):
- self.write("hello world")
- settings = {
- }
- application = Application([
- (r"/", MainHandler),
- ], **settings)
- if __name__ == "__main__":
- http_server = HTTPServer(application)
- http_server.listen(8081)
- IOLoop.instance().start()
- nginx.conf:
- user root;
- worker_processes 1;
- error_log /var/nginx_error.log;
- pid /var/run/nginx.pid;
- events {
- worker_connections 51200;
- use epoll;
- }
- http {
- # Enumerate all the Tornado servers here
- upstream frontends {
- server 127.0.0.1:8081;
- server 127.0.0.1:8082;
- server 127.0.0.1:8083;
- server 127.0.0.1:8084;
- }
- #include /etc/nginx/mime.types;
- default_type application/octet-stream;
- access_log /var/log/nginx/access22.log;
- keepalive_timeout 65;
- proxy_read_timeout 200;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- gzip on;
- gzip_min_length 1000;
- gzip_proxied any;
- gzip_types text/plain text/html text/css text/xml
- application/x-javascript application/xml
- application/atom+xml text/javascript;
- # Only retry if there was a communication error, not a timeout
- # on the Tornado server (to avoid propagating "queries of death"
- # to all frontends)
- proxy_next_upstream error;
- server {
- listen 8085;
- # Allow file uploads
- client_max_body_size 50M;
- location ^~ /static/ {
- root /var/www;
- if ($query_string) {
- expires max;
- }
- }
- location = /favicon.ico {
- rewrite (.*) /static/favicon.ico;
- }
- location = /robots.txt {
- rewrite (.*) /static/robots.txt;
- }
- location / {
- proxy_pass_header Server;
- proxy_set_header Host $http_host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Scheme $scheme;
- proxy_pass http://frontends;
- }
- }
- }
The above is the test method for implementing WEB in Python.