-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pingserver_full.py
48 lines (34 loc) · 1.4 KB
/
test_pingserver_full.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
"""Module containing full tests for the pingserver"""
import os
from threading import Thread
from time import sleep
from socket import create_connection, gethostbyname, gethostname
def test_get_request():
"""test the pingserver by getting multiple addresses"""
local_address = gethostbyname(gethostname())
main_file = __file__.replace("\\", "/").rsplit("/", maxsplit=2)[0] + "/pingserver/main.py"
Thread(target=os.system, args=("python3 " + main_file + " -a " + local_address,),
daemon=True).start()
sleep(3)
assert _get_address(local_address) == "1.1.0.0"
assert _get_address(local_address) == "1.2.0.0"
assert _get_address(local_address) == "1.3.0.0"
assert _get_address(local_address) == "1.4.0.0"
assert _get_address(local_address) == "1.5.0.0"
def _get_address(local_address):
"""Get one address from ther pingserver"""
sock = create_connection((local_address, 20005))
_send(sock, "GET address")
return _recv(sock)
def _send(sock, text):
"""Send string to the given socket"""
sock.send(_string_to_bytes(text))
def _recv(sock):
"""Receive string from the given socket"""
return _bytes_to_string(sock.recv(4096))
def _string_to_bytes(input_text):
"""Convert string to bytes object"""
return bytes(input_text, 'utf-8')
def _bytes_to_string(input_bytes):
"""Convert bytes object to string"""
return input_bytes.decode()