-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
executable file
·155 lines (97 loc) · 3.91 KB
/
api.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
#!/usr/bin/env python
# This is a Python implementation of an APE client
import json
import urllib
import string
import random
import socket
class APEClient:
def __init__(self, host, port=80):
self.sessid = None
self.pipeid = None
self.host = host
self.port = port
self.protocol = 1 # the APE protocol
self.chl = 1
self.name = self.identifier(16)
self.login()
def identifier(self, length):
nums = '0123456789'
return ''.join(random.choice(string.letters + nums) for i in xrange(length))
def connect(self):
self.con = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.con.connect((self.host, self.port))
def send_cmd(self, cmd, params):
self.connect()
data = [{'cmd': cmd.upper(),
'chl': self.chl,
'params': params}]
if self.sessid:
data[0]['sessid'] = self.sessid
self.chl += 1
data_json = json.dumps(data, False, True, True, True, json.JSONEncoder, None, (',',':'))
url = '/'+str(self.protocol)+'/?'
self.con.send("POST "+url+" HTTP/1.1\r\n")
self.con.send('Host: '+self.host+':'+str(self.port)+"\r\n")
self.con.send("Accept-Encoding: identity\r\n")
self.con.send("Content-type: application/x-www-form-urlencoded; charset=utf-8\r\n")
self.con.send("Content-Length: "+str(len(data_json))+"\r\n")
self.con.send("\r\n")
self.con.send(data_json+"\n\n")
def recv_raw(self):
# TODO do some non-blocking stuff so we can safely receive more than 4 kb without blocking
data = self.con.recv(4096)
self.con.close()
idx = string.find(data, "\r\n\r\n")
if(idx < 0):
raise Exception("could not find JSON data in server response")
raws_json = data[idx:len(data)-1]
raws = json.loads(raws_json)
return raws
def get_raw(self, raws, name):
name = name.upper()
for raw in raws:
if raw['raw'] == name:
return raw
return None
def login(self):
self.send_cmd('connect', {'name': self.name})
raws = self.recv_raw()
raw = self.get_raw(raws, 'login')
if not raw:
raise Exception("No login raw returned")
if not raw['data'] or not raw['data']['sessid']:
raise Exception("Did not receive sessid from server")
self.sessid = raw['data']['sessid']
def join(self, channel):
self.send_cmd('join', {'channels': channel})
raws = self.recv_raw()
raw = self.get_raw(raws, 'channel')
if not raw or not raw['data'] or not raw['data']['pipe'] or not raw['data']['pipe']['pubid']:
raise Exception("Did not receive pipe pubid from server")
self.pipeid = raw['data']['pipe']['pubid']
def send(self, data):
data = urllib.quote(data)
self.send_cmd('send', {
'msg': data,
'pipe': self.pipeid})
def recv(self):
raws = self.recv_raw()
raw = self.get_raw(raws, 'data')
if not raw or not raw['data'] or not raw['data']['msg']:
raise Exception("Problem with received data")
data = raw['data']['msg']
return urllib.unquote(data)
# =======================
# end code to be librarified
# ======================
# Example usage of library
#
# NOTE: A simple web-client that sends and receives on the 'foo' channel
# is available here: http://gui.grafiki.org/
ape = APEClient('1.ape.gui.grafiki.org', 6969)
# If the channel doesn't exist, it will be automatically created
ape.join('foo')
ape.send("Wohooo!") # send a string. the string is escaped, so you can safely send JSON data
data = ape.recv() # blocks until data is received. this unescapes the string data before returning it
print data