-
Notifications
You must be signed in to change notification settings - Fork 4
/
hello_trace.py
executable file
·172 lines (140 loc) · 4.85 KB
/
hello_trace.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
# hello_trace.py: demo application for otrace to compute reciprocal of a number
import BaseHTTPServer
import copy
import logging
import SocketServer
import sys
import traceback
import urlparse
if sys.version_info[0] < 3:
def encode(s):
return s
def decode(s):
return s
else:
def encode(s):
return s.encode("utf-8")
def decode(s):
return s.decode("utf-8")
Page_template = """<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello Trace</title>
</head>
<body>
<h2>Hello Trace</h2>
<form method="get" action="/">
Find reciprocal of:
<input id="number" name="number" type="text" autocomplete="off" autofocus="autofocus"></input>
<input type="submit" value="Submit" />
</form>
<p>
<span>%s</span>
</body>
</html>
"""
# Request statistics
Request_stats = {"count":0, "path":""}
class GetHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
# Process GET request
path_comps = urlparse.urlparse(self.path)
query_args = urlparse.parse_qs(path_comps.query)
logging.warning("path=%s", self.path)
# Update request statistics
Request_stats["count"] += 1
Request_stats["path"] = self.path
if path_comps.path == "/favicon.ico":
self.send_error(404)
return
# Retrieve user input
number = query_args.get("number", [None])[0]
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
if number is None:
# No user input; display input form
resp = Page_template % ""
else:
# Process user input and display response
recv = Receive(number)
try:
resp = Page_template % recv.respond(self)
except Exception, excp:
logging.error("ERROR: %s", excp)
resp = "Server error:\n" + "".join(traceback.format_exception(*sys.exc_info()))
self.wfile.write(encode(resp))
def log_message(self, format, *args):
# Suppress server logging
return
class MultiThreadedServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
pass
class Receive(object):
def __init__(self, value):
self.value = float(value)
def respond(self, request):
# Respond to request by computing reciprocal and returning response string
# Diagnostic print (initially commented out)
##if self.value <= 0.001:
## print("Client address", request.client_address)
# Trace assertion (initially commented out)
##import otrace
##otrace.traceassert(self.value > 0.001, label="num_check")
# Compute reciprocal of number
response = "The reciprocal of %s is %s" % (self.value, 1.0/self.value)
return response
def __deepcopy__(self, memo):
return self.__class__(copy.deepcopy(self.value, memo))
Http_addr = "127.0.0.1"
Http_port = 8888
def submit(number, timeout=None):
"""Simulate user form submission by executing a HTTP request"""
import urllib2
def http_request():
try:
response = urllib2.urlopen("http://%s:%s/?number=%s" % (Http_addr, Http_port, number))
resp_str = decode(response.read())
return "\n".join(resp_str.split("\n")[-4:-3]) if resp_str.startswith("<html>") else resp_str
except Exception, excp:
return excp.reason if isinstance(excp, urllib2.URLError) else str(excp)
if not timeout:
return http_request()
# HTTP request with timeout (run in a separate thread)
import threading
import Queue
exec_queue = Queue.Queue()
def execute_in_thread():
exec_queue.put(http_request())
thrd = threading.Thread(target=execute_in_thread)
thrd.start()
try:
return exec_queue.get(block=True, timeout=timeout)
except Queue.Empty:
return "Timed out after %s seconds" % timeout
def test_fun():
# Test function that raises an exception
raise Exception("TEST EXCEPTION")
def run_server(args=[]):
http_server = MultiThreadedServer((Http_addr, Http_port), GetHandler)
print >> sys.stderr, "Listening on %s:%s (^C to stop)" % (Http_addr, Http_port)
http_server.serve_forever()
def main(args=[]):
# Run HTTP server
logging.warning("hello_trace: args="+str(args))
try:
run_server(args)
except KeyboardInterrupt:
pass
def trace_main(args=[]):
# Run HTTP server with otrace
import otrace
# Start otrace (in its own thread)
oshell = otrace.set_trace(globals(), new_thread=True)
try:
run_server(args)
except KeyboardInterrupt:
# Clean shutdown of otrace (to avoid hung threads)
oshell.shutdown()
if __name__ == "__main__":
trace_main(args=sys.argv[1:])