How to create a site in python without using frameworks?
Just in general, tell me what to do. Details myself Googling
p.s. I know that is the creation of a bike, but that I needed to study the operation of the web application in particular and education in general programming
Answer 1, Authority 100%
Excellent question – nothing Cons
.
The project directory
Create a folder ./cgi-bin
write to a file ./ cgi-bin / index.py with the contents of
#! / Bin / python3
print ( "Content-Type: text / html") # Heather response
print () # A blank line immediately after Heather
# And then the response was
print ( "& lt; TITLE & gt; CGI script output & lt; / TITLE & gt;")
print ( "& lt; H1 & gt; This is my first CGI script & lt; / H1 & gt;")
print ( "Hello, world!")
Make the script executable
chmod + x ./cgi-bin/index.py
And zapukay http server (available with apache or lighttpd)
python3 -m http.server --cgi 8000
Follow a link http: // localhost: 8000 / cgi-bin / index.py here and Hello-Word in pure python is ready.
Option little more complicated with one module
import http.server
class Handler (http.server.BaseHTTPRequestHandler):
def do_GET (self):
self.send_response (200, 'OK')
self.send_header ( 'Content-Type', 'text / html')
self.end_headers ()
self.wfile.write ( '' '
& Lt; TITLE & gt; Handler script output & lt; / TITLE & gt;
& Lt; H1 & gt; This is my first Handler script & lt; / H1 & gt;
Hello, world!
'' '.Encode ())
httpd = http.server.HTTPServer (( '', 8000), Handler)
httpd.serve_forever ()
There’s no URI handler and one answer will be on all pages.
Answer 2, Authority 44%
The Python standard library module has http.server . This HTTP-server, from which you can create sites of any complexity. Of course, the greater the complexity, the more you will have to write the functionality that already exists in the framework. But if you need a server for debugging, or you want to understand how it is arranged, start with him.
Answer 3, Authority 22%
If it is direct without the framework, the cgi. But why? There are also frameworks.
-
Flask
Flask – framework for creating the language of the Python programming web applications using Werkzeug tool kit and template Jinja2. Falls into the category of so-called microframework [en] – minimalist carcasses Web applications deliberately provide only the most basic features
.
-
Django
Django (Django, [ ‘dʒæŋɡoʊ] [6]) – a free framework for web applications in Python, using the MVC design pattern [7]. The project is supported Django Software Foundation.