Last updated: Aug 9, 2019
IMAGE GALLERY (3)
external program outputs HTML instead of plain text, the browser-shell detects it and renders it as a web page. Webserver invokes external programs
A browser is an HTTP client because it sends requests to an HTTP server (Web server), which then sends responses back to the client. The standard (and default) port for HTTP servers to listen on is 80, though they can use any port.
- Apache creates processes and threads to handle additional connections. While Nginx is said to be event-driven, asynchronous, and non-blocking.
- Nginx includes advanced load balancing and caching abilities.
Nginx is a lot lighter than Apache
Python webframeworks provide simple to use
Request-Responsestructure for HTTP calls
search the codebase
grep -i wsgi
- WSGI - Web Server Gateway Interface
- standardise interface b/w webserver and python applications/frameworks
- PEP 333(3)
- Makes it easy to switch webserver like
apacheis they speak the WSGI language
What happens when you click on a URL?
- Browser contacts the HTTP web server for demanding the URL
- Parsing the URL
- Look for the filename
- If it finds that file, a request is sent back
- Web browser takes a response from the web server
- As the server response, it either shows the received file or an error message.
CGI - Common Gateway Interface
Standard how client can communicate with HTTP server. In
Execute programs that execute like command-line-interface programs1.
It is possible to set up the HTTP server so that whenever a file in a certain directory is requested that file is not sent back; instead it is executed as a program, and whatever that program outputs is sent back for your browser to display. This function is called the Common Gateway Interface or CGI and the programs are called CGI scripts. These CGI programs can be a Python Script, PERL Script, Shell Script, C or C++ program, etc.
- Static webserver serves boring static HTML files.
- Session Management not possible
- No dynamic views
- Invoke a
SCRIPTinstead of a
FILE. Scripts are saved on server. UNIX like philosohpy, request inputs are set in
environment variables. Can use
WSGIlanguage and feeds the framework with requests and responses paameters
Making a python web-server from scratch
Client Request ----> Nginx (Reverse-Proxy) | /|\ | | `-> App. Server I. 127.0.0.1:8081 | `--> App. Server II. 127.0.0.1:8082 `----> App. Server III. 127.0.0.1:8083
CGI programming with Python
Generally CGi scripts are installed in
cgi-bin folder, for security reasons2.
#!/usr/bin/env python import cgi import cgitb; cgitb.enable() # for troubleshooting print "Content-type: text/html" print print """ <html> <head><title>Sample CGI Script</title></head> <body> <h3> Sample CGI Script </h3> """ form = cgi.FieldStorage() message = form.getvalue("message", "(no message)") print """ <p>Previous message: %s</p> <p>form <form method="post" action="index.cgi"> <p>message: <input type="text" name="message"/></p> </form> </body> </html> """ % cgi.escape(message)
- Shebang header to invoke python interpreter
Enable traceback from CGI scripts
apt install -y nginx ufw sudo ufw app list sudo ufw allow 'Nginx Full' sudo ufw status sudo cp -v /path/project/. /var/www/ /etc/nginx sites-enabled sites-available python3 -m http.server --cgi 8000 python -m webbrowser http://localhost:8000/cgi-bin/hello.py
Security is very important, the user you used to run CGI server will be used to run all the CGi scripts, some of them could be dangerous.
vue + nginx
try npm run build and then there file be a dist folder. copy the contents of the folder to the server.