-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebapi.py
More file actions
56 lines (53 loc) · 1.81 KB
/
webapi.py
File metadata and controls
56 lines (53 loc) · 1.81 KB
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
import json
import socket
import time
import io
CHUNK_SIZE = 1024 * 32
class WebApiCall:
def __init__(self, method_name, host, port, **kwargs):
self.method_name = method_name
self.host = host
self.port = port
self.args = kwargs
def __call__(self,**kwargs):
for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except OSError as msg:
s = None
print(msg)
continue
try:
s.connect(sa)
except OSError as msg:
s.close()
s = None
print(msg)
continue
break
if s is None:
print('Could not open socket')
with s:
request = {}
request["name"] = self.method_name
request["args"] = dict(self.args, **kwargs)
s.sendall(json.dumps(request).encode("UTF-8"))
data = s.recv(1)
if len(data) == 0:
raise RuntimeError("No data received")
if data[0] != 0:
raise RuntimeError("Error code: {0}".format(data[0]))
else:
read = io.StringIO()
data = s.recv(CHUNK_SIZE).decode("UTF-8")
while data:
read.write(data)
data = s.recv(CHUNK_SIZE).decode("UTF-8")
read.seek(0)
return read.read()
class Steam:
def __init__(self, api_key, ip, port):
self.api = api_key
self.get_schema = WebApiCall("steam_get_schema", ip, port, key=api_key)
self.get_items = WebApiCall("steam_get_items", ip, port, key=api_key)