This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pseudo-server.py
executable file
·68 lines (57 loc) · 1.72 KB
/
pseudo-server.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
import asyncore, socket
import time, sys
import modules
import config
currts = lambda: int(time.time())
class Sock (asyncore.dispatcher):
def __init__ (self, name="default"):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.name = name
conf = config.link[name]
self.connectedto = conf['remote']
self.connect (self.connectedto)
self.sid = conf['sid']
self.conf = conf
self.sendq = ''
self.recvq = ''
self.protocol = modules.load(conf['protocol'], self)
def push (self, msg):
self.sendq += '%s\r\n' % msg
def handle_connect (self):
print '!!!\tConnecting to %s:%d!' % (self.connectedto)
if hasattr(self.protocol, 'connect'):
self.protocol.connect()
def handle_close (self):
print '!!!\tClose.'
modules.unload(self.protocol)
self.close()
def handle_read (self):
self.recvq += self.recv(8192)
for i in xrange(self.recvq.count('\r\n')):
messages = self.recvq.split('\r\n')
message = messages.pop(0)
self.recvq = '\r\n'.join(messages)
print 'Recv:\t%s' % message
if message != '':
self.protocol.parse(message)
def writable (self):
return len(self.sendq) > 0
def handle_write (self):
sent = self.send (self.sendq)
for line in self.sendq[:sent].split('\r\n'):
if line is not '':
print 'Send:\t%s' % line
self.sendq = self.sendq[sent:]
if __name__ == '__main__':
print 'JStoker\'s Pseudo-Server - Version 0.1.0'
print
if len(sys.argv) != 2:
print 'Usage: %s <name of config block>' % sys.argv[0]
print 'where <name of config block> is replaced with the name of the config block you wish to connect to.'
print
else:
s = Sock(sys.argv[1])
asyncore.loop()
print 'Pseudo-Server will now quit.'
sys.exit(0)