I am trying to run a simple web server with Python. It works fine when I run with below command.
python -m http.server 8000
But when I make a Windows service with same code, it fails with error 1053.
sc create TestWebServer binPath="C:\Python37\python.exe -m http.server 8000" start=auto
I found that some relative posts say debug can solve this problem, but it doesn't work for me.
sc create TestWebServer binPath="C:\Python37\python.exe -m http.server 8000 debug" start=auto
Edited
I made a python code like below.
import os
from http.server import HTTPServer, CGIHTTPRequestHandler
os.chdir('.')
server = HTTPServer(server_address=('', 8000),
RequestHandlerClass=CGIHTTPRequestHandler)
server.serve_forever()
And I made it as Windows service, but it still fails with error 1053.
sc create TestWebServer binPath="C:\Python37\python.exe C:\TestWebServer.py" start=auto
Related
I am using python http.server 80 to expose my downloaded files to my twilio whatsapp bot
is there a way that as my django-twillio app starts it automatically runs the server on port 80 as well
python -m http.server 80
Adding this code to your django-twilio app will programmatically start your server on localhost:80
from http.server import HTTPServer, SimpleHTTPRequestHandler
httpd = HTTPServer(('localhost', 80), SimpleHTTPRequestHandler)
httpd.serve_forever()
I use python3 -m http.server --bind localhost to quickly host local versions of web content for testing.
I was using this to host a web version of a game I was working on and ran into this error:
Uncaught ReferenceError: SharedArrayBuffer is not defined
The engine I'm using has advice for this error:
Enable the following HTML response headers on the website you're hosting your project on:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
But I don't understand how to set these headers. Do I need to implement my own subclass of BaseHTTPRequestHandler (and how)? Or do I call send_header on... something?
I was trying to run my webpage in localhost. I am using python http.server to run the code in localhost. It works for GET requests but not working for POST.
python -m http.server --cgi 8080
It is showing
"Error code: 501
Message: Can only POST to CGI scripts.
Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation."
CGI scripts have to be in a special directory, like cgi-bin:
Only directory-based CGI are used
cgi_directories¶
This defaults to ['/cgi-bin', '/htbin'] and describes directories to treat as containing CGI scripts.
So you can use them this way:
In a terminal, run: python3 -m http.server --cgi 8080
and in another one:
$ cat cgi-bin/foo.py
#!/usr/bin/env python3
import sys
print('200 OK\r\n\r\nfoo')
$ chmod u+x cgi-bin/foo.py
$ curl http://localhost:8080/cgi-bin/foo.py -X POST
foo
I am asking it because I write very simply app but IT DON'T WORK.
I wrote this command in terminal in the /dir:
python3 -m http.server --cgi
My script is in dir/cgi-bin/hp.py and that code:
#!/usr/bin/env python3
print"Content-type: text/html"
print()
print"<h1>Hello world!</h1>"
This I saw in window of browser:
Error response
Error code: 403
Message: CGI script is not executable ('/cgi-bin/hp.py').
Error code explanation: HTTPStatus.FORBIDDEN - Request forbidden --
authorization will not help."
How can I fix it?
Here are the following steps which I tried to reproduce the problem:
# app.py
print("Content-type: text/html")
print()
print("<h1>Hello world!</h1>")
Created a file app.py in cgi-bin directory
Used command to run http.server with cgi
python -m http.server --bind localhost --cgi 8000
I tried accessing the path "http:localhost/cgi-bin/" got Error 403
Now the resolving part, which is opening the link in browser.
I ran the command:
python -mwebbrowser http://localhost:8000/cgi-bin/app.py
After the while it gave me the result, and I was able to access the link for the step 2 also.
I hope that helps you.
Result:
I'm trying to set up Python's CGIHTTPServer on Mac OS X to be able to serve CGI scripts locally, but I seem to be unable to do this.
I've got a simple test script:
#!/usr/bin/env python
import cgi
cgi.test()
It has permissions -rwxr-xr-x# and is located in ~/WWW (with permissions drwxr-xr-x). It runs just fine from the shell and I have this script to serve them using CGIHTTPServer:
import CGIHTTPServer
import BaseHTTPServer
class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
cgi_directories = ["~/WWW"]
PORT = 8000
httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "serving at port", PORT
But when I run it, going to localhost:8000 just serves the content of the script, not the result (i.e. it gives back the code, not the output).
What am I doing wrong?
The paths in cgi_directories are matched against the path part of the URL, not the actual filesystem path. Setting it to ["/"] or [""] will probably work better.