-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdrequest.py
executable file
·77 lines (69 loc) · 2.51 KB
/
cmdrequest.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
#!/usr/bin/env python
import sys
import json
import struct
import requests
# Read a message from stdin and decode it.
def getMessage():
rawLength = sys.stdin.buffer.read(4)
if len(rawLength) == 0:
sys.exit(0)
messageLength = struct.unpack('@I', rawLength)[0]
message = sys.stdin.buffer.read(messageLength).decode('utf-8')
return json.loads(message)
# Encode a message for transmission,
# given its content.
def encodeMessage(messageContent):
# https://docs.python.org/3/library/json.html#basic-usage
# To get the most compact JSON representation, you should specify
# (',', ':') to eliminate whitespace.
# We want the most compact representation because the browser rejects # messages that exceed 1 MB.
encodedContent = json.dumps(messageContent, indent = 4, separators=(',', ':')).encode('utf-8')
encodedLength = struct.pack('@I', len(encodedContent))
return {'length': encodedLength, 'content': encodedContent}
# Send an encoded message to stdout
def sendMessage(encodedMessage):
sys.stdout.buffer.write(encodedMessage['length'])
sys.stdout.buffer.write(encodedMessage['content'])
sys.stdout.buffer.flush()
def handleRequest(myRequest):
if 'urlParams' not in myRequest:
myRequest.update({'urlParams': {}})
if 'headers' not in myRequest:
myRequest.update({'headers': {}})
if myRequest['type'] == "GET":
response = requests.get(url = myRequest['url'], params = myRequest['urlParams'], headers = myRequest['headers'])
else:
response = requests.post(url = myRequest['url'], params = myRequest['urlParams'], headers = myRequest['headers'], json = myRequest['body'])
if (response.status_code == 200):
return {
"errorCode": response.status_code,
"data": response.json()
}
else:
return {
"errorCode": response.status_code,
"data": response.reason
}
while True:
receivedMessage = getMessage()
'''
receivedMessage = {
"type": "POST",
"url": "https://postman-echo.com/post?a=aaaaa",
"urlParams": { "a1": "a1v", "a2": "a2v" },
"body": {
"body": {
"bodyTitle": "title example",
"info": "info example"
}
},
"headers": {
"headerOne": "headerOneValue",
"headerTwo": "headerTwoValue",
"content-type": "myzzz"
}
}
'''
response = handleRequest(receivedMessage)
sendMessage(encodeMessage(response))