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 e0e1071

Browse files
committed
Initial commit
1 parent 714edac commit e0e1071

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

‎README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Adding REST API to Python App
2+
3+
Just some example code using timeouts in BaseHTTPServer to add a REST API to a
4+
Python app.
5+
6+
Currently, the app will exit once a POST request is sent to the web server on
7+
port 1024. There is no additional processing being done, but whatever it is,
8+
should be done fairly quickly to allow the app to check for any pending HTTP
9+
requests.
10+
11+
No authentication enabled, no SSL. Not Production ready!
12+
13+
Python 2.x, should be easily convertable to Python 3

‎app.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/local/bin/python
2+
3+
import BaseHTTPServer
4+
5+
addy = ('',1024)
6+
7+
# A global data structure for access between HTTP server and main code
8+
class Config(object):
9+
httpd_running = False
10+
config = Config()
11+
12+
# define the http handler
13+
class httpd_handler(BaseHTTPServer.BaseHTTPRequestHandler):
14+
def do_GET(self):
15+
print("In GET Handler for ",self)
16+
self.wfile.write(self.__dict__)
17+
self.wfile.write(config.__dict__)
18+
return
19+
20+
def do_POST(self):
21+
print("In POST Handler for ",self)
22+
self.wfile.write(self.__dict__)
23+
self.wfile.write(config.__dict__)
24+
config.httpd_running = False
25+
return
26+
27+
# create http daemon
28+
httpd = BaseHTTPServer.HTTPServer(addy,httpd_handler)
29+
httpd.timeout = 0.1
30+
31+
print "Starting HTTP Server"
32+
config.httpd_running = True
33+
while config.httpd_running:
34+
# Check for any pending HTTP events
35+
httpd.handle_request()
36+
# Run the rest of the program
37+
print "Stopped HTTP Server"

0 commit comments

Comments
(0)

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