Simple Python Django Website

I am trying to create a website using the Django framework. I looked at the tutorial on the Django project site, but it contains a lot of information that I do not need. I have python scripts that provide output, and I need to have this output on the Internet. My question is how to simply manage a Django link that launches a script and provides its output on the Internet, or maybe you provide a link where I can read about it?

Thanks.

+4
source share
6 answers

"I have python scripts that provide output, and I need to have this output on the Internet."

This is not what Django does. What you want to do can be achieved with something simple:

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer class Handler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write("magic content goes here") if __name__=="__main__": try: server = HTTPServer(("", 8080), Handler) server.serve_forever() except KeyboardInterrupt: server.socket.close() 

Watch the line self.wfile.write . Everything you write ends in a browser. If that matters, you can use self.path in Handler to check which file was requested.

Tested with Python 2.6.4, server access with Chrome browser.

+4
source

All are right. This is the wrong way to use django. However, if you need a stop gap measure when you convert your script to the correct idiom:

 import sys from django.http import HttpResponse def cgi_view(request, my_module): __import__(my_module) mod = sys.modules[my_module] text = mod.main() resp = HttpResponse(text) # Then set your headers on resp return resp 

I leave this as an exercise to figure out how to set the headers. Sorry for being too lazy, but I have to go to work.

PS if your script does not take into account all its output functions in the main () function, you can use the subprocess to get the result.

+1
source

Use the mod_wsgi plugin for Apache.

You can do this to see how an existing script can be converted to a WSGI application. This is a starting point to show how the WSGI works.

 import sys def myWSGIApp( environ, start_response ): with file( "temp", "w" ) as output: sys.stdout= output execfile( "some script.py" ) sys.stdout= __stdout__ status = '200 OK' headers = [('Content-type', 'text/plain')] start_response(status, headers) result= file( "temp", "r" ) return result 

Note that you can easily rewrite your scripts to meet the WSGI standard, too. This is not the best approach yet.

If you have this

 if __name__ == "__main__": main() 

You just need to add something like this to each script.

 def myWSGIApp( environ, start_response ): with file( "temp", "w" ) as output: sys.stdout= output main() sys.stdout= __stdout__ status = '200 OK' headers = [('Content-type', 'text/plain')] start_response(status, headers) result= file( "temp", "r" ) return result 

Then each script can be called as a WSGI application and can be connected to the basis of WSGI.

The best approach is to rewrite your scripts so that they do not use sys.stdout , but write to a file that is passed to them as an argument.

The test version of your server can be simple.

 from wsgiref.simple_server import make_server httpd = make_server('', 8000, myWSGIApp) 

When you have WSGI applications for your scripts, you can create a smarter WSGI Application that

  • Parses the URL. Updates the environment called script to run.

  • Launches your WSGI application with the appropriate environment.

See http://docs.python.org/library/wsgiref.html for information.

You can then configure Apache to use the WSGI server using mod_wsgi .

See http://code.google.com/p/modwsgi/ for more details.

+1
source

This is not how Django works. Make a tutorial, you will save a lot of time and frustration.

0
source

I have python scripts that provide output, and I need to have this output on the Internet.

What is Django? Use a CGI script in python (maybe you already have one) or a WSGI application (which is a bit more difficult to deploy)

0
source

Django is a framework work. Just use CGI scripts.

0
source

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


All Articles