Last change
on this file since a5ce597 was
025e4da,
checked in by Alvaro Neira Ayuso <aneira@…>, 5 years ago
|
Remove unnecessary variables from test server
|
-
Property mode set to
100644
|
File size:
1.4 KB
|
Rev | Line | |
---|
[05b1088] | 1 | # |
---|
| 2 | # Copyright (C) 2020 Soleta Networks <info@soleta.eu> |
---|
| 3 | # |
---|
| 4 | # This program is free software: you can redistribute it and/or modify it under |
---|
| 5 | # the terms of the GNU Affero General Public License as published by the |
---|
| 6 | # Free Software Foundation, version 3. |
---|
| 7 | # |
---|
| 8 | |
---|
[bb65bd7] | 9 | import socket |
---|
| 10 | import sys |
---|
| 11 | |
---|
| 12 | class Server(): |
---|
| 13 | |
---|
| 14 | _probe_json = '{"id": 0, "name": "test_local", "center": 0, "room": 0}' |
---|
| 15 | _probe_msg = 'POST /probe HTTP/1.0\r\nContent-Length:'+ \ |
---|
| 16 | str(len(_probe_json)) + \ |
---|
| 17 | '\r\nContent-Type:application/json\r\n\r\n' + _probe_json |
---|
| 18 | |
---|
| 19 | def __init__(self, host='127.0.0.1', port=1234): |
---|
| 20 | self.host = host |
---|
| 21 | self.port = port |
---|
| 22 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
| 23 | self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
---|
| 24 | |
---|
| 25 | def connect(self, probe=True): |
---|
| 26 | try: |
---|
| 27 | self.sock.bind((self.host, self.port)) |
---|
| 28 | except socket.error as msg: |
---|
| 29 | print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' |
---|
| 30 | + msg[1]) |
---|
| 31 | sys.exit() |
---|
| 32 | |
---|
| 33 | self.sock.listen(10) |
---|
| 34 | self.conn, self.addr = self.sock.accept() |
---|
| 35 | if probe: |
---|
| 36 | self.send(self._probe_msg) |
---|
| 37 | return self.recv() |
---|
| 38 | |
---|
| 39 | def send(self, msg): |
---|
| 40 | self.conn.send(msg.encode()) |
---|
| 41 | |
---|
| 42 | def recv(self): |
---|
| 43 | return self.conn.recv(1024).decode('utf-8') |
---|
| 44 | |
---|
| 45 | def stop(self): |
---|
| 46 | self.conn.close() |
---|
| 47 | self.sock.close() |
---|
Note: See
TracBrowser
for help on using the repository browser.