-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
68 lines (52 loc) · 1.48 KB
/
client.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
# -*- encoding: utf-8 -*-
"""
Bottle Client
This files contains an example of a Bottle client
"""
import socket
class Bottle(object):
"""Connects into the bottle server
"""
def __init__(self, host="localhost", port=42000):
self.server = (host, port,)
self.sock = None
def _connect(self):
if self.sock is None:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(self.server)
def _send(self, message):
self._connect()
self.sock.send(message + "\n")
response = self.sock.recv(4096)
return response.strip()
def use(self, queue):
msg = "USE {}".format(queue)
response = self._send(msg)
if response == "OK":
return True
raise Exception(response)
def put(self, data):
msg = "PUT {}".format(data)
response = self._send(msg)
if response == "OK":
return True
raise Exception(response)
def get(self):
response = self._send("GET")
if response == "NULL":
return None
return response
def close(self):
if self.sock is not None:
self.sock.close()
self.sock = None
conn = Bottle()
conn.use("emails")
for v in range(1, 99999):
conn.put("mensagem de teste {}".format(v))
while True:
data = conn.get()
if data is None:
break
print "Data from server: {}".format(data)
conn.close()