-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathremote_caller.py
46 lines (34 loc) · 1.1 KB
/
remote_caller.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
import socket
from urllib import parse as urllib
from xmlrpc import client as xmlrpclib
from config import scgi
class SCGIRequest:
def __init__(self):
try:
host, port = scgi.split(":")
addrInfo = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)
self.sInfo = addrInfo[0][:3]
self.scgi = addrInfo[0][4]
except Exception:
self.sInfo = socket.AF_UNIX, socket.SOCK_STREAM
self.scgi = scgi
def send(self, methodName, params):
xmlRequest = xmlrpclib.dumps(params, methodName)
scgiRequest = self.addHeaders(xmlRequest).encode("utf-8")
s = socket.socket(*self.sInfo)
s.connect(self.scgi)
s.send(scgiRequest)
sFile = s.makefile()
data = response = sFile.read(1024)
while data:
data = sFile.read(1024)
response += data
s.close()
scgiResponse = urllib.unquote(response)
xmlResponse = "".join(scgiResponse.split("\n")[4:])
return xmlrpclib.loads(xmlResponse)[0][0]
@staticmethod
def addHeaders(body):
headers = "CONTENT_LENGTH\x00{}\x00SCGI1\x00".format(len(body))
encodedHeaders = "{}:{},".format(len(headers), headers)
return encodedHeaders + body