Django on Strato webspace - python

I have a web space package at Strato and they say that I can use Python on their webspace. Currently I use PHP with CakePHP since PHP works, but I'd rather use Python with some framework, where Django seems to be the match.
So I uploaded a little script into /cgi-bin/test.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
print "Content-Type: text/html"
print
print "Hello, World!"
And it indeed prints Hello, World!.
Then I tried to import django below the “Hello, World!“:
try:
import django
except Exception as e:
print e
All I got is an “Internal Server Error“.
Does it make sense to use this server for Python at all, or is that just some half hearted support that will not be fun anyway?

The import doesn't work because you haven't set up the required environment variable. However, even if you managed to get the import working, Django deployment is a bit more involved than putting a script in your cgi-bin directory. You need to choose a method such as WSGI, or FastCGI, and configure your webserver (Apache/Nginx/etc).
You have three categories of hosts for deploying Django.
a shared web host that is Django friendly. I can thoroughly recommend djangohosting.ch, which I used until I started working for a web hosting company myself.
a VPS (this can be more tricky, because you have to manage more yourself, but you will have more flexibility and better performance that shared hosting)
a platform as a service (PaaS) such as ep.io or heroku.

Related

Setup for running simple python scripts on (Nginx) web server?

I would like to run a couple of very simple Python 3 scripts on the web. As an example, say, the script just reads certain parameters from the URL, sends an email and prints out a simple HTML page saying "success".
I have a virtual private server with Nginx, so I am free to setup the best framework.
My question is: what is the state-of-the-art setup to do this?
More particularly: what do I need to install on my server for Nginx and what do I use in my Python scripts for e.g. reading the URL content? My idea is, that once the server setup is done, I can just put any new script_xy.py file into some directory and it can be accessed using the URL of the script, without a full blown deployment for each script.
Flask If I were to use Flask (or Django), each new script would need its own, continuously running process and its own Nginx setup. That seems like a total overkill to me. Another alternative is web2py, is it the same here or would that be an idea?
CGI 20 years ago I used to program such simple things as Perl scripts using CGI. I read this can be done in principle with Python, but CGI is slow. Then there is Fast CGI. However, my impression was that this is still a bit outdated?
WSGI WSGI seems to be the state-of-the-art alternative to CGI for Python. What python modules would I need to import in my script and what would be the setup for Nginx?
Something else? As you see, I might just need a few hints on what to search for. I am not even sure if I need to search for "Python web framework" or "Python server" etc.
Any help is greatly appreciated.
Thanks a lot for your ideas!
juxeku

Can the Pyramid framework be run on a CGI server

Ok, I get that CGI is sow outdated, and no one likes it, but I have a customer who needs a new site, is on shared hosting, and does not wish to change hosting providers. We are doing the back-end of his site in Python, and are researching Python framework options that can run as CGI(no one wants to just use the CGI module). So far, It seems Django can be run on CGI, but it is terrible. Flask and Bottle seem to run well on CGI(at least it is supported), but I was wondering if the Pyramid Framework can. If not, I guess we will just be using Flask. Can I get Pyramid running on CGI? If so what would be the performance against running Flask r Bottle under CGI?
See this solution here, it shows how to use WSGI on CGI
https://www.python.org/dev/peps/pep-0333/#the-server-gateway-side
Take a look at wsgiref as well
https://docs.python.org/2/library/wsgiref.html
example :
https://github.com/w1mvy/twitter_bot_on_gae-py/blob/55754d0ec1580b67f7f0c710f5cf3456313cff4d/app.py

Python with MAMP not working

I'm in the process of switching from PHP over to Python.
The web project I'm on is very very small (probably just two to three script files) so I would prefer to avoid any framework and just work with Python.
I'm running MAMP (not the Pro version) on OS X Yosemite.
My python test file works fine when executed in the terminal.
But when I open it in the browser, it just shows the text connect of the file, not the output.
PyTest.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
# enable debugging
import cgitb
cgitb.enable()
print 'Content-Type: text/plain'
print
print 'Hello, World!'
I'm really new to Python. What am I missing?
Thanks!
I realized that I did not completely understand the way Python and Webservers interact and why it wouldn't work with MAMP out of the box.
I'm now using a lightweight web framework (bottle) which has a small development server built in.
The website later will be deployed using NGINX and uWSGI.

Can I run Python 2.7 Libraries (Impacket, Pcapy) on Django

Hi I just want to ask if its possible to run Impacket to Django,
On my project I am already done on my sniffing and parsing using Impacket and Pcapy but my clients requested that the GUI will be web based. I picked Django because its the most widely used and all but I am having doubts that it can run my Libraries.
For starters can Django open my NIC in Ubuntu and have access to sniff on it?
or is it better for me to use Flask for from what I read flask is being run on the Python Console Application, from what I understood I will install a HTTP Server in the Project then the Python Console will be like a Controller (MVC) to my GUI which is Flask.
You can run anything from Django, it just provides a framework to get stuff to the web.
As long as the Django application is running as a user which has privileges to access your NIC there won't be an issue with that.
You can simply call your code you already have from the Django Views.
The time this stuff takes may be too long for a web request so you may need to pass some stuff down a message queue and look up the results. Look at Celery for that purpose.
I do prefer Flask over Django but it doesn't matter what you use.
Just remember Django is another library it all still runs inside Python :)

Simple Python server setup

I am trying to learn python (coming from PHP), and want to set up the simplest web server so I can start coding.
I found the integrated HTTP server, so I figured it should be the easiest way.
root#ubuntu:/var/py# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
The webserver is working, accessing http://test.dev:8000/test.py (thanks /etc/hosts) works - but shows me the contents of the file ( print('Hello world!'); ), without interpreting it.
How to properly set up the server / interpretor ?
Python is for writing programs in general, not only web-sites.
SimpleHTTPServer is really just a trivial HTTP server, it serves files. It doesn't try to interpret code.
If you want to do web-development with Python, you should be using a web framework. web.py is a good choice, you can check its tutorial. Another option is Django, which also has a brilliant tutorial.
Both of them have simple development servers built in, so you'll be able to start experimenting easily.
If you are just starting out with python I would recommend you start with scripts that can be run from the console using python interpreter. (eg: python run1.py)
Once you have mastered the basics, you can move onto web programming. (I am guessing that you want to try web programming since you mention a web server.) In this case, you have multiple options (all of which work with Apache):
Django framework : Really good framework, has a built-in webserver and has fantastic documentation (http://www.djangobook.com/en/2.0/index.html).
You need to know python basics first
WSGI: https://code.google.com/p/modwsgi/ : Apache module for running python code

Categories