9

I'm trying to write a simple server in python. So after watching tutorial, I'm trying to import a few modules.

from http.server import HTTPServer
from http.server import SimpleHTTPServer

As the doc says, it has been moved, that's why i'm doing so.

But it gives me this error : from http.server import SimpleHTTPServer ImportError: cannot import name 'SimpleHTTPServer'

And without SimpleHTTPServer I can't use SimpleHTTPRequestHandler, as it is defined in SimpleHTTPServer.SimpleHTTPRequestHandler.

How can I resolve this ?

Danial
  • 542
  • 2
  • 9
  • 24
  • 3
    The `SimpleHTTPServer` was moved to *be* the module `http.server`. You want to use the request handler class `BaseHTTPRequestHandler`. From the [docs](https://docs.python.org/3/library/http.server.html). – rassar Feb 19 '20 at 17:37
  • 2
    If you're using Python 3 it's probably best to refer to _those_ docs, not the ones for Python 2. – ChrisGPT was on strike Feb 19 '20 at 17:38
  • thanks.. it worked ... you should post it as answer.. @rassar – Danial Feb 19 '20 at 17:40

2 Answers2

19

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.

rassar
  • 5,412
  • 3
  • 25
  • 41
2

My solution was:

python -m http.server

If I type python3 on the console is printed "Python not found". (I'm on Windows)

Alessandro
  • 99
  • 2
  • 9
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 14 '22 at 11:41