-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_server.py
93 lines (74 loc) · 2.52 KB
/
http_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# helper to load url
# runs webserver and loads url with webbrowswer module
# Based on the force.py Networkx/D3.js sample application
# Copyright (C) 2011-2016 by
# Aric Hagberg <hagberg@lanl.gov>
# Dan Schult <dschult@colgate.edu>
# Pieter Swart <swart@lanl.gov>
# All rights reserved.
# BSD license.
# Modifications to be used within Jupyter by
# David Zellhoefer <david@8bit-inferno.de>
import sys
import os
def load_url(path):
print("Webserver working directory: "+os.getcwd())
sys.stdout.flush()
PORT = 8000
httpd = StoppableHTTPServer(("127.0.0.1",PORT), handler)
thread.start_new_thread(httpd.serve, ())
webbrowser.open_new('http://localhost:%s/%s'%(PORT,path))
input("Press <RETURN> to stop server\n")
httpd.stop()
print("To restart server run: \n%s"%server)
if sys.version_info[0] == 2:
import SimpleHTTPServer, BaseHTTPServer
import socket
import thread
import webbrowser
handler = SimpleHTTPServer.SimpleHTTPRequestHandler
input = raw_input
server = "python -m SimpleHTTPServer 8000"
class StoppableHTTPServer(BaseHTTPServer.HTTPServer):
def server_bind(self):
BaseHTTPServer.HTTPServer.server_bind(self)
self.socket.settimeout(1)
self.run = True
def get_request(self):
while self.run:
try:
sock, addr = self.socket.accept()
sock.settimeout(None)
return (sock, addr)
except socket.timeout:
pass
def stop(self):
self.run = False
def serve(self):
while self.run:
self.handle_request()
else:
import http.server, http.server
import socket
import _thread as thread
import webbrowser
handler = http.server.SimpleHTTPRequestHandler
server = "python -m http.server 8000"
class StoppableHTTPServer(http.server.HTTPServer):
def server_bind(self):
http.server.HTTPServer.server_bind(self)
self.socket.settimeout(1)
self.run = True
def get_request(self):
while self.run:
try:
sock, addr = self.socket.accept()
sock.settimeout(None)
return (sock, addr)
except socket.timeout:
pass
def stop(self):
self.run = False
def serve(self):
while self.run:
self.handle_request()