i have running a python webserver by using this simple script -
from http.server import SimpleHTTPRequestHandler as RH
from socketserver import TCPServer
ServerName='localhost'
OnPort=8000
print ("Server is running at Port 8000")
TCPServer((ServerName, OnPort), RH).serve_forever()
it is running good and run my index.html file but it is not run .py file in browser when i type --
http://localhost:8000/myfile.py
it just show my python codes as it is i write in file ,it is not execute the code please help me to run my python file (.py) in browser by using this webserver only ,i don't want to use any framework or another webserver.
Is there any way to make a virtual host in this python server like apache.
if possible please suggest me that how to do this and any configuration file need to be configured or not.
thanx...
The problem is that SimpleHTTPRequestHandler only serves files out of a directory, it does not execute them. You must override the do_GET method if you want it to execute code.
You might want to check out CGIHTTPRequestHandler instead. I very briefly played with it on a linux based system and the CGI criteria was for a file to be executable and have the correct shabang. I'm not sure if this would work on Windows though ( if that is even a relevant concern )
With your example code you would only need to change RH to
import CGIHTTPServer.CGIHTTPRequestHandler as RH
Alternatively the 3rd party library Twisted has a concept of .rpy files that are mostly plain Python logic. http://twistedmatrix.com/trac/wiki/TwistedWeb
Note:
Just verified that the CGIHTTPRequestHandler works. Caveats is that all python files must be in a cgi-bin subdir, they must have a valid shabang, must be executable, and must provide valid CGI output.
Personally having written C++ CGI scripts in the 90's, the CGI route seems like the path to maddness... so check out Twisted, CherryPy, or Django ( those three mostly cover the Python web spectrum of possibilities )
Related
i was going to make a simple web interface for a Python script / module I am working on. I figured to submit the form which would do 1 of 2 options. Modify a config json file and run the python script, or submit a form to the python script and execute it.
Simply put, i know when working with some other applications, Apache for example, you need to configure it such that it accepts .py files as executable. Is that the same case with SimpleHTTPServer? If so, how?
I was looking at: https://docs.python.org/2/library/simplehttpserver.html
I was also looking at BaseHTTPServer at https://docs.python.org/2/library/basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler though nothing was jumping out at me during configuration
you can enable the CGI (Common Gateway Interface https://en.wikipedia.org/wiki/Common_Gateway_Interface ) of the SimpleHTTPServer, see the following thread How to host python cgi script with `python -m SimpleHTTPServer 8000` or `python -m CGIHTTPServer 8000`?
this is similar to get Django running ( https://en.wikipedia.org/wiki/Django_(web_framework) ) . Django is enabled using the mod_wsgi module or FastCGI on a WSGI-compatible webserver (Apache,...).
I'm using Community Edition PyCharm 4.5.1 and I'm developing CGI python scripts. My needs are to start the debugger and attach the script (then break to the first breakpoint) once it is called by my HTTP client.
I don't know if I can, I hope. Everything works fine, from the Python server to the HTML/JavaScript code that calls my CGI script. Also, I'm perfectly able to debug a Python script I just start normally. But now the problem is that it is the HTTP server that starts the script, neither me (from command line) nor the debugger itself.
Any idea? Thanks!
AFAIK, you just put into your script
import cgitb
cgitb.enable()
print "Content-type: text/html\n\n"
and it's being debugged by itself.
I have meet the same problem as yours when i use pycharm on CentOS, but i found pycharm can attach to cgi script automaticly on windows, so i try to follow the source code of CGIRequestHandler, I found there is a difference in CGIRequestHandler.run_cgi() function, it will use fork on linux, and subprocess on windows, so i guess may be these two different ways of creating child process leading to different result. so i try the following code, force it subprocess on linux, and it works!
CGIHTTPRequestHandler.have_fork = 0
httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
I'm working with a simple website (a few html files and one Python script) that's running on my LAN. In Chrome I can pull up the HTML files and Python scripts through port 80, as normal and I am using WSGIScriptAlias commands in /Library/Server/Web/Config/apache2/httpd_wsgi.conf that are working and I've set up the site and specified for it to be allowed to use Python apps through the Server GUI application.
For several reasons, I'm using a different port number for this site. If I go to http://mycomputer.lan:1234/myfile.html, I can see the HTML file. But if I go to http://mycomputer.lan:1234/MyWSGIApplicationScript, the server (the latest version, got it installed today) reports:
Not Found
The requested URL /LandSearch was not found on this server.
I've seen this work before, on other servers and I remember setting it up and getting it working on another system running OS X so the wsgi scripts worked fine on a non-standard port, but I don't have access to the notes and information I had at that time. That makes me suspect it's probably a simple configuration option I need to change for the server to find and use the Python scripts from a different port.
What do I need to reconfigure to get it to use wsgi scripts on a non-standard port?
Even AppleCare didn't have an answer for this one.
When I first set up the site, I enabled the 'Python "Hello World" app at /wsgi'. This is in the advanced settings:
I did that just for testing, so when I set up the site again, I didn't bother with it. It turns out that this one setting does more than enable one wsgi application. It turns out that, by default, the file /Library/Server/Web/Config/apache2/httpd_wsgi.conf is not read by Apache while setting up a virtual host. But checking the box to enable this one wsgi webapp means that the following line:
Include /Library/Server/Web/Config/apache2/httpd_wsgi.conf
will be included in the configuration file for this particular virtual host. Any scripts aliases defined with the WSGIScriptAlias command in that file will now be available to your website, no matter what port your website is on.
this may sound strange but i can't seem to find an example anywhere..
i'm trying to run a script via CGI locally but i'm trying to run it as an .exe via py2exe.
so the idea is that you click the executable and CGI starts locally and the script gets run without having to install any python on the machine and you can pull up the local website in browser and the script is producing the webpage.
So i can get all these things to work on my machine with python installed, and i created a script that imports the cgi script i created to start CGI and imports the python script that i want to display and i can see compiled versions of both in the dist that it creates. but from here i have no idea where to go, as normally i would just ping the cgi-bin/script.py folder via the local host and it would show up in my browser.
my question:
can a CGI and script be run locally and displayed as an executable without having python on the machine.
and the extra build file with bdist.win32 created does that need to be called somehow?
i'm assuming there is somewhere in the main program i'm supposed to 'display' the script via html but not quite sure where to do that?
i get the 'cgi script is not a plain file ('//') error.. not sure on that one.
any help appreciated, thx
I partially solved this by importing webbrowser and then openning the local url with
webbrowser.open_new('http://localhost:8086') #proper url here
But it's precisely CGI what is driving me crazy. For some reason, when browsing to a python script, it won't execute - I just get the code, even when the original python script defined cgi_directories properly (they did work with the python script). I hope that helps a bit, at least. Here's the whole script:
#!/usr/bin/env python
import webbrowser
import BaseHTTPServer
import CGIHTTPServer
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8008)
handler.cgi_directories = ["/cgi"]
httpd = server(server_address, handler)
webbrowser.open_new("http://localhost:8008/cgi/script.py");
httpd.serve_forever()
Hello a client of mine bought a pretty bad web host and i don't even have ssh access, their ticket suport only answered with "yes we support python in our servers" but i can't run any .cgi .py or application.wsgi files. is there a sure way to tell if the server supports python?
I only have access to the ftp and the directadmin interface, i would like to know more before i can complaing again to their support system otherwise they will not pay attention.
The host is neubox.net this is what i already tried.
This tutorial http://www.howtoforge.com/embedding-python-in-apache2-with-mod_python-debian-etch worked on my dev machine, it says that i need to add a webhost in the apache2 /available-sites dir but obviously i don't have access to that folder in the hosting.
I also tried putting this script on the root of my host, called application.wsgi it didnt work
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I also tried this file application.py on the root
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
print "Content-Type: text/plain;charset=utf-8"
print
print "Hello World!"
Those files were shown as plain text, i tried that exact same code but named application.cgi and oddly enough it returned a 404 error, the file of course its there.
I saw at the directadmin interface in site summary that
CGI-Bin OFF
So i guess thats the reason for the 404.
In the same page i see that the name servers are
http://ns115.neubox.net/
http://ns116.neubox.net
The first one says
Apache is functioning normally
This is their services comparison site (spanish) http://neubox.net/comparativo-hosting.php
I wish i could know what OS are they running i'm almost sure is linux because on my root there is a folder .htpasswd and those .folders are linux for hidden, but i'm not sure if thats a sure way to tell.
They gave me this url http://72.249.55.33/info.php its for phpinfo() i see fast-cgi but all the tuts about it talk about doing things like changing Apache configuration wich i obviously can't do, this is the end of my search right? they do not support python.
In your server thay sais that they have php, so maybe you can use this php function. to retrive more info, executing a python script:
# hacking.py
import sys
print sys.version_info
and after you make something like this
<?php
// echo $path = exec('pwd');
// exec python script
echo exec('python hacking.py');
?>
dont forget the file permissions
sacabuche has the correct answer. That script is a very simple way to test for Python support. It works on pretty much every flavor of linux and Berkely UNIX (BSD), a version of which is underlying OSX on a Mac Pro. If that script is not working for you, chances are it's a permissions issue.
I would continue working through the support staff on the host to get this resolved. If they can't help, you've got the wrong host. I've changed hosts several time due to lack of support, or misrepresented features, such as separate folder hierarchies per domain... something that's getting increasingly hard to find.
Currently I am on MochaHost, which I do recommend, since they seem to have it all together so far.
-Jack