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)
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.
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()
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 )
I running apache with mod_fastcgi to run python scripts (using Ubuntu OS).
How can I debug python with fastcgi? I put a pdb.set_trace() call in my script, but when loading the page in a browser nothing happens in the console from which I ran the apache start command (/etc/init.d/apache2 start).
Where should the python debugger run??
Dan
You have to attach pdb to a running instance of your FCGI script, read this for a How To.
This information is also stated in the FCGI FAQ.
It is a rather strange 'bug'.
I have written a cherrypy based server. If I run it this way:
python simple_server.py > out.txt
It works as expected.
Without the the redirection at the end, however, the server will not accept any connection at all.
Anyone has any idea?
I am using python 2.4 on a Win XP professional machine.
Are you running the script in an XP "command window"? Otherwise (if there's neither redirection nor command window available), standard output might simply be closed, which might inhibit the script (or rather its underlying framework).
CherryPy runs in a "development" mode by default, which includes logging startup messages to stdout. If stdout is not available, I would assume the server is not able to start successfully.
You can change this by setting 'log.screen: False' in config (and replacing it with 'log.error_file: "/path/to/error.log"' if you know what's good for you ;) ). Note that the global config entry 'environment: production' will also turn off log.screen.