Set Up Python Simplehttpserver on Windows

Set up Python simpleHTTPserver on Windows

From Stack Overflow question What is the Python 3 equivalent of "python -m SimpleHTTPServer":

SimpleHTTPServer is for python2, so you're getting the error.

In python3, The following works:

python -m http.server [<portNo>]

Because using Python 3, the module SimpleHTTPServer has been replaced by http.server, at least in Windows.

How to start python simpleHTTPServer on Windows 10

Ok, so different commands is apparently needed.

This works:

C:\pathToIndexfile\py -m http.server

As pointed out in a comment, the change to "http.server" is not because of windows, but because I changed from python 2 to python 3.

How to setup SimpleHTTPServer with a DNS on Windows

I solved this myself. It is indeed a Windows thing. I added "127.0.0.1" to the list of DNS Servers in the configuration of my network device.

windows python http.server can not be reached

It happened to be a localhost binding problem.

python -m http.server --bind localhost

solved it. Python response was:

Serving HTTP on 127.0.0.1 port 8000 (http://127.0.0.1:8000/) ...

Hope it can help someone else!

What is the Python 3 equivalent of python -m SimpleHTTPServer

From the docs:

The SimpleHTTPServer module has been merged into http.server in Python 3.0. The 2to3 tool will automatically adapt imports when converting your sources to 3.0.

So, your command is python -m http.server, or depending on your installation, it can be:

python3 -m http.server

SimpleHTTPServer not found python3

The SimpleHTTPServer module was moved to be the module http.server. So the command is:

python3 -m http.server

Also, the new SimpleHTTPRequestHandler object is BaseHTTPRequestHandler.

Cannot run python SImpleHTTPServer on windows 10 for d3.js testing (issue changing environment variable)

The python variable is incorrect - rather you should add the python executable path to the PATH environment variable e.g.

PATH=C:\Python27\;C:\Python27\Scripts;

You should then be able to call python from anywhere.

Indeed any path you include in the PATH will then allow you to call executables in that path without specifying the full path or extension.

i.e.

C:\Python27\python.exe becomes python

C:\WINDOWS\system32\cmd.exe becomes cmd

For more detail see: https://en.wikipedia.org/wiki/PATH_(variable) or my answer to this question: Error in Process.Start() -- The system cannot find the file specified

Running a Python web server as a service in Windows

This is what I do:

Instead of instancing directly the class BaseHTTPServer.HTTPServer, I write a new descendant from it that publishes an "stop" method:

class AppHTTPServer (SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
def serve_forever(self):
self.stop_serving = False
while not self.stop_serving:
self.handle_request()

def stop (self):
self.stop_serving = True

And then, in the method SvcStop that you already have, I call that method to break the serve_forever() loop:

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.httpd.stop()

(self.httpd is the instance of AppHTTPServer() that implements the webserver)

If you use setDaemon() correctly on the background threads, and interrupt correctly all the loops in the service, then the instruction

exit(0)

in SvcStop() should not be necessary

Is SimpleHTTPServer available if Python is installed?

No, this will not be sufficient in 2018, and even less so in the future. You should tell them to use python3 -m http.server, and that Python 3 is required, but with a note saying they can use SimpleHTTPServer from Python 2 if they have that instead—and probably mentioning than Mac users do already have Python 2 instead.

(I'd like to say that you can get away with ignoring Python 2 entirely, because anyone who chooses to use it ought to know what they're doing by this point, but thanks to OS X, you can't quite get away with that yet.)

  • Windows: Users who go to python.org to install Python will be presented with 3.6 as the first choice, and 2.7 as the second choice. And in a year and a half, 2.7 will likely be moved to the "old versions" page along with 3.5 and 2.6.
  • Mac: Python 2.7 comes preinstalled. If they go to python.org, or use Homebrew, they'll be presented with 3.6 as the default, but if they know that they already have Python, they won't try that. And Apple may well keep shipping 2.7 long beyond 2020.
  • Linux: Current versions of most distros have 3.x preinstalled, and some do not have 2.x. And in a year and a half, most of the big ones are planning on relegating 2.x to community-package status. However, there are plenty of people still using older "Long-Term Support" versions of their distros, and these will continue to support (and maybe pre-install) 2.7 until they go out of support, which ranges from 2-8 years from now.

How to set up simple HTTP server in Python 3.5 on Windows 10?

I have been able to get the code you have written working without issue, so perhaps it is an issue in your calling environment.

If you see this section of the cgi docs: https://docs.python.org/3/library/cgi.html then

If it’s installed in the standard cgi-bin directory, it should be possible to send it a request by entering a URL into your browser of the form:

http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home

as well as the cgi section of the python HTTP Server docs here: https://docs.python.org/3/library/http.server.html#http.server.CGIHTTPRequestHandler

cgi_directories


This defaults to ['/cgi-bin', '/htbin'] and describes directories to treat as containing CGI scripts.

What we need then is a certain folder structure, as in:

cgi-test
|
+-- cgi-bin
|
+-- test.py

with test.py containing your code as above.

Now to start the python HTTP Server navigate to your enclosing folder (ie cgi-test) and type in python -m http.server --cgi 8000 and assuming your python executable is on your system path you should get a response like: Serving HTTP on 0.0.0.0 port 8000 ...

Finally navigate using your browser to http://localhost:8000/cgi-bin/test.py to see your rendered page.

Note: cgi-test could be anything, for instance C:\Users\Username\Website, D:\MyServer\ ...

Note2: If I move the test.py file up one level to the cgi-test folder I get a result similar to what you describe (ie I see the code rather than the rendered page)

Note3: I am running on Windows 7 with python 3.4, but it should hopefully be similar enough that you should be able to follow the same procedure.



Related Topics



Leave a reply



Submit