-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshop.py
103 lines (93 loc) · 3.58 KB
/
shop.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
import json
import sys
import BaseHTTPServer
import httplib
import traceback
import socket
from BaseHTTPServer import BaseHTTPRequestHandler
promotionPort = 80
def sendAndVerify(ip, port, uri):
print "start to invoke %s" % uri
httpClient = None
try:
httpClient = httplib.HTTPConnection(ip, port, timeout=30)
headers = {"Content-type": "application/x-www-form-urlencoded"
, "Accept": "text/plain"}
httpClient.request("GET", uri, None, headers);
response = httpClient.getresponse()
retStatus = response.status
if retStatus != 200:
print "Test fail, status code is %s" % retStatus
return bool('False')
return bool('True')
except Exception, e:
print "Test fail, status exception is %s" % e
traceback.print_exc()
return bool('False')
finally:
if httpClient:
httpClient.close()
def getLocalIP():
return [(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]
class TodoHandler(BaseHTTPRequestHandler):
localIP = getLocalIP()
def do_GET(self):
if self.path == '/api/v6/shop/items':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('localIP', self.localIP)
self.end_headers()
msg = {
"items":
[{"itemId":"001", "itemName":"cloth"},
{"itemId":"002", "itemName":"cloth1"},
{"itemId":"003", "itemName":"cloth2"}
]
}
self.wfile.write(json.dumps(msg))
elif self.path == '/api/v6/shop/order':
if sendAndVerify("promotion", promotionPort, "/api/v6/promotion/query"):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('localIP', self.localIP)
self.end_headers()
msg = {
"userId": "1234",
"itemId": "002"
}
self.wfile.write(json.dumps(msg))
else:
self.send_response(500)
self.send_header('Content-type', 'application/json')
self.end_headers()
msg = {"exception":"Error invoke %s" % "/api/v6/promotion/query"}
self.wfile.write(json.dumps(msg))
elif self.path == '/api/v6/product/deliver':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('localIP', self.localIP)
self.end_headers()
msg = {
"itemId": "002",
"destination": "shenzhen"
}
self.wfile.write(json.dumps(msg))
elif self.path == '/health':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
msg = {
"status":"UP"
}
self.wfile.write(json.dumps(msg))
else:
self.send_error(404, "{\"message\":\"File %s not found.\"}" % self.path)
return
if __name__ == '__main__':
# Start a simple server, and loop forever
ServerClass = BaseHTTPServer.HTTPServer
hostPort = int(sys.argv[1])
print "host port is %s"%hostPort
server = ServerClass(('0.0.0.0', hostPort), TodoHandler)
print "Starting shopService, use <Ctrl-C> to stop"
server.serve_forever()