-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnixSocketClient.py
55 lines (44 loc) · 1.59 KB
/
UnixSocketClient.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
import socket
import sys
import pickle
class UnixSocketClient:
def __init__(self, name):
# Create a UDS socket
global sock
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# define non-blocking socket
sock.setblocking(0)
# Connect the socket to the file where the server is listening
server_address = "uds_" + name
print >>sys.stderr, 'connecting to %s' % server_address
try:
sock.connect(server_address)
except socket.error, msg:
print >>sys.stderr, msg
#I can't connect to the server, something is wrong I'll exit
sys.exit(1)
def send(self, messageObj):
global sock
try:
# serialize object and prepare for transfer
out = pickle.dumps(messageObj, pickle.HIGHEST_PROTOCOL)
# print >>sys.stderr, 'sending "%s"' % message
# Send data
sock.sendall(out + "\n")
except socket.error, msg:
print >>sys.stderr, msg
sys.exit(1)
def close(self):
print "closing connection"
global sock
# Strange thing, if I don't send something before closing Tornado will cut off
# last information :|
sock.sendAll("\n")
sock.close()
# TODO this part of code is not yet finished, receive will call handler in his parent
def receive(self):
try :
data = sock.recv(1024)
return data
except :
return None