Repository: electricitymaps/electricitymaps-contrib

Issue:
Mockserver server.py does not work in Python 3
```mockserver_1  | Traceback (most recent call last):
mockserver_1  |   File "../server.py", line 5, in <module>
mockserver_1  |     from SimpleHTTPServer import SimpleHTTPRequestHandler, BaseHTTPServer
mockserver_1  | ModuleNotFoundError: No module named 'SimpleHTTPServer'
```
Looks like some modules have been reorganized in Python 3.  Hopefully this is just a matter of updating the imports.


Discussion:
Have you tried to install simplehttpserver using pip? Maybe add the pip
install command in the docker file and rebuild the mockserver?

On Jan 5, 2018 22:44, "systemcatch" <notifications@github.com> wrote:

> mockserver_1  |   File "../server.py", line 5, in <module>
> mockserver_1  |     from SimpleHTTPServer import SimpleHTTPRequestHandler, BaseHTTPServer
> mockserver_1  | ModuleNotFoundError: No module named 'SimpleHTTPServer'
>
> Looks like some modules have been reorganized in Python 3. Hopefully this
> is just a matter of updating the imports.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/tmrowco/electricitymap/issues/979>, or mute the thread
> <https://github.com/notifications/unsubscribe-auth/ABlEKHc7BFiHVZarAF6vd-wXtBZK8mYlks5tHpfYgaJpZM4RU_AR>
> .
>

More info: https://stackoverflow.com/questions/7943751/what-is-the-python-3-equivalent-of-python-m-simplehttpserver
Running 2to3 made just one change to server.py.
```
-from SimpleHTTPServer import SimpleHTTPRequestHandler, BaseHTTPServer
+from http.server import SimpleHTTPRequestHandler, BaseHTTPServer
```
But I still get `ImportError: cannot import name 'BaseHTTPServer'` when running `python3 server.py`
Looks like 2to3 can't auto fix this.
According to this:
https://stackoverflow.com/questions/23264569/python-3-x-basehttpserver-or-http-server
apparently `BaseHTTPServer` is now `http.server`.
Maybe the correct import is
```python
from http.server import SimpleHTTPRequestHandler, HTTPServer
```

More info:
https://docs.python.org/3/library/http.server.html

Hopefully this can point you in the right direction. Unfortunately I don't have time to fix this right now but will be able to after Wednesday.
  
Yeah I've been looking through those docs.  I've made the following changes so that it will run on the commandline.
```python
from http.server import SimpleHTTPRequestHandler, HTTPServer, test
```
```python
if __name__ == '__main__':
    test(CORSRequestHandler, HTTPServer)
```
```bash
chris@ThinkPad:~/electricitymap/mockserver$ python3 server.py 
Serving HTTP on 0.0.0.0 port 8000 ...
```
But no luck getting it to work on the map yet.
I think you need to do a `docker-compose build` first in order to make sure to rebuild the docker image using python 3
  

Resolving patch:
diff --git a/mockserver/server.py b/mockserver/server.py
--- a/mockserver/server.py
+++ b/mockserver/server.py
@@ -2,7 +2,7 @@
 
 # Usage: python __file__.py <port>
 
-from SimpleHTTPServer import SimpleHTTPRequestHandler, BaseHTTPServer
+from http.server import SimpleHTTPRequestHandler, HTTPServer, test
 
 class CORSRequestHandler(SimpleHTTPRequestHandler):
     def do_OPTIONS(self):
@@ -15,4 +15,4 @@ def end_headers(self):
         SimpleHTTPRequestHandler.end_headers(self)
 
 if __name__ == '__main__':
-    BaseHTTPServer.test(CORSRequestHandler, BaseHTTPServer.HTTPServer)
+    test(CORSRequestHandler, HTTPServer)


Test patch: