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()
Related
I created a web server in Python 3.6 using http.server.HTTPServer, http.server.SimpleHTTPRequestHandler and socketserver.ThreadingMixIn. It works as expected and I can access the webpage from any device on the local network.
I compiled it with PyInstaller to create a Windows executable. The webpage works with localhost, but it is not accessible from any device on the local network.
I used nmap from another device to scan the computer hosting the web server, and it appears that the port used by the webserver (8080) is open when I run my script normally (with the Python interpreter), and everything works. However, when I use the executable produced by PyInstaller the port isn't open and the webpage not reachable.
The executable doesn't produce any errors, and apart from that everything works.
I have tried to run the .exe file as administrator, and to this disable my antivirus/firewall. It doesn't work.
Here is my PyInstaller command :
pyinstaller --runtime-tmpdir "" --onefile -i icon.ico script.py
And here is the relevant code in my python file :
import http.server
import socketserver
port = 8080
class ThreadingSimpleServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
pass #using this so the webserver can handle mutliple requests at a time
class myWebServer(http.server.SimpleHTTPRequestHandler):
def do_POST(self):
#does stuff
try:
server = ThreadingSimpleServer(('', port), myWebServer)
server.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down the program.')
server.socket.close()
I run your code and it works for me. The O.S. ask about opening the connection and everything seems to be ok. You must produce a log of some kind in order to let us understand what is going wrong. In my run i omit the -i switch because I don't have a .ico file.
Ok so I solved the issue but I still don't understand what caused it. Since the beginning I was building my script in a folder located in AppData : it wasn't working and I had no Windows firewall warning/confirmation showing up.
I moved my script to the Desktop and compiled it here : the Windows firewall confirmation showed up (this one) and the executable worked well.
I thought maybe the firewall might have flagged my program earlier and all the subsequent builds were blacklisted or something like that so I tried to compile the script in the original location, but this time I renamed it and I deleted all the files generated previously : it didn't work either (and no firewall confirmation/warning).
So now i'm pretty sure the error was caused by the firewall, and it had to do with the location from where the build was done, but I don't really understand why.
The following script generates a 500 internal error when I try to access is via a web browser.
#!/usr/bin/python
import cgi
import cgitb
import paramiko
cgitb.enable() # Enable error messages
print "Content-Type: text/html" # HTML will follow
print ""
print "<html><head></head><body>Done.</body></html>"
The script runs without an error if I execute it from the linux command line.
If I comment out the "import paramiko" line, it runs fine in the web browser.
I know that paramiko is installed and working because it is used by other command line scripts. This is my first attempt to use paramiko from a script accessed by a web interface.
I don't see any errors in the /var/log/httpd/error_log file.
Where should I be looking to fix this issue?
I'm running Centos 5-10.el5, with apache v 2.2.3-83.el5 and python 2.7.2.
Changing the first line in my script to:
#!/usr/local/bin/python2.7
...resolved my issues. The web scripts now use the same python that I get when running scripts from the command line.
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 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 )
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