Python with MAMP not working - python

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.

Related

Why is my python script not running in shared hosting cpanel GoDaddy

I have shared hosting for my website at Godaddy with shared hosting in cpanel. I made a python script that print hello and I even tried with the GUI too but it is not printing. It is only showing the code. I called GoDaddy and they said to do print("Hello World") but even though I did it is still not working. Right now my code is print("Hello World"). Printing hello world like that normally works but it is not working. I need to use python 2.6.6 because that's what GoDaddy gives as the version number for python. Please can somebody help?
You can check out my python script in my website at My python script in my website.
You use Apache Web server, and it doesn't process you python script. It consider it as static file and return it's content.
There are a few ways to make it work
mod_python for your Apache server
Use CGI
Make apach proxy to python web server by your choice (e.g. gunicorn)
I found out that python is installed on it but it doesn't let you run python scripts in godaddy economy hosting cpanel. Thanks!

Flask on Raspberry Pi changing standard page

On my RPi (b model) Flask is installed.
I can access the webpage "It works" via another browser on another computer.
Works great. As well on my LAN as from the outside world.
And also, I can write and run Python scripts and let it act as server and so on.
But how do I enable my own Python script for Flask as standard at boot from RPi.
It's seated in the /home/pi/myproject dir and I'd like it to be standard.
I can't find the standard python script and templates to replace.
Any help is welcome.

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

Importing Python files into each other on a web server

Using CGI scripts, I can run single Python files on my server and then use their output on my website.
However, I have a more complicated program on my computer that I would like to run on the server. It involves several modules I have written myself, and the SQLITE3 module built in Python. The program involves reading from a .db file and then using that data.
Once I run my main Python executable from a browser, I get a "500: Internal server error" error.
I just wanted to know whether I need to change something in the permission settings or something for Python files to be allowed to import other Python files, or to read from a .db file.
I appreciate any guidance, and sorry if I'm unclear about anything I'm new to this site and coding in general.
FOLLOW UP: So, as I understand, there isn't anything inherently wrong with importing Python files on a server?
I suggest you look in the log of your server to find out what caused the 500 error.
You can get an extensive error message by adding the following lines at the beginning of your CGI script:
import cgitb
cgitb.enable()

Django on Strato webspace

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.

Categories