Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0af588d

Browse files
committed
Python 2.x and 3.x support
1 parent e0e1071 commit 0af588d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

‎app.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/usr/local/bin/python
22

3-
import BaseHTTPServer
3+
try:
4+
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
5+
except:
6+
from http.server import BaseHTTPRequestHandler, HTTPServer
47

8+
# listen on port 1024 of the localhost
59
addy = ('',1024)
610

711
# A global data structure for access between HTTP server and main code
@@ -10,28 +14,28 @@ class Config(object):
1014
config = Config()
1115

1216
# define the http handler
13-
class httpd_handler(BaseHTTPServer.BaseHTTPRequestHandler):
17+
class httpd_handler(BaseHTTPRequestHandler):
1418
def do_GET(self):
15-
print("In GET Handler for ",self)
16-
self.wfile.write(self.__dict__)
17-
self.wfile.write(config.__dict__)
19+
print(("In GET Handler for ",self))
20+
self.wfile.write(str(self.__dict__).encode())
21+
self.wfile.write(str(config.__dict__).encode())
1822
return
1923

2024
def do_POST(self):
21-
print("In POST Handler for ",self)
22-
self.wfile.write(self.__dict__)
23-
self.wfile.write(config.__dict__)
25+
print(("In POST Handler for ",self))
26+
self.wfile.write(str(self.__dict__).encode())
27+
self.wfile.write(str(config.__dict__).encode())
2428
config.httpd_running = False
2529
return
2630

2731
# create http daemon
28-
httpd = BaseHTTPServer.HTTPServer(addy,httpd_handler)
32+
httpd = HTTPServer(addy,httpd_handler)
2933
httpd.timeout = 0.1
3034

31-
print"Starting HTTP Server"
35+
print("Starting HTTP Server")
3236
config.httpd_running = True
3337
while config.httpd_running:
3438
# Check for any pending HTTP events
3539
httpd.handle_request()
3640
# Run the rest of the program
37-
print"Stopped HTTP Server"
41+
print("Stopped HTTP Server")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /