Easy setup to create web pages in pure Python

I am completely new to Python, and I would like to start creating web pages with it (but not using a web framework or template module). What are the minimum requirements? Could you suggest a simple setup?

Thanks!

EDIT: I'm not trying to be minimalist at all costs. I am looking for a simple general solution that remains close to the language (and does not impose a design paradigm such as MVC).

+6
source share
4 answers

I am going with a stream and I would recommend using a lightweight structure.

Firstly, web applications expose your server to security risks, so it’s useful to use what is supported by a more or less large community of developers (more eye boxes to fix vulnerabilities).

Also, if you want to "stay close to the language", you need some level of abstraction to control HTTP in python mode. Python is the volume level [battery included].

Some of the frameworks remain very close to the syntax, semantics, and style of python. Take a look at webpy , for example. I think this quote talks about most of the webpy related philosophy:

"Django allows you to write web applications in Django. TurboGears allows you to write web applications in TurboGears. Web.py allows you to write web applications in Python." - Adam Atlas

Another good candidate in terms of brevity and the use of "regular" python is cherrypy . On its website:

CherryPy allows developers to create web applications in much the same way as for any other object-oriented Python program. [...] Your CherryPy-based web applications are actually stand-alone Python applications that implement their own multi-threaded web server. You can deploy them wherever you can run Python applications.

+3
source

To clear the WSGI application without a full structure:

from wsgiref.simple_server import make_server def application(environ, start_response): # Sorting and stringifying the environment key, value pairs response_body = ['%s: %s' % (key, value) for key, value in sorted(environ.items())] response_body = '\n'.join(response_body) status = '200 OK' response_headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) return [response_body] # Instantiate the WSGI server. # It will receive the request, pass it to the application # and send the application response to the client httpd = make_server( 'localhost', # The host name. 8051, # A port number where to wait for the request. application # Our application object name, in this case a function. ) # Wait for a single request, serve it and quit. httpd.handle_request() 

Then you can use nginx: http://wiki.nginx.org/NgxWSGIModule

This is the most stable, safe and easiest bone setting.

Additional examples: https://bitbucket.org/lifeeth/mod_wsgi/src/6975f0ec7eeb/examples/ .

This is the best way to find out (as you requested). I have already gone this way.

+4
source

If you really want this, you need a minimum understanding of WSGI , which is the glue between Python and the web server. You can create a Python web application directly on top of this.

But, like the other defendants, I really urge you to use the framework. They are not all huge monolithic things like Django - see, for example, microframes like Flask . They will take care of such obvious things as routing URL requests to the correct code.

+3
source

Just run this command

 $ python -m SimpleHTTPServer Serving HTTP on 0.0.0.0 port 8000 ... 

doesn't get much easier than

now browse the CGIHTTPServer.py in the Python library for more ideas

+2
source

Source: https://habr.com/ru/post/892200/


All Articles