VoxelServer/tools/test_client.py

50 lines
1.2 KiB
Python
Raw Permalink Normal View History

2018-11-24 18:04:53 +01:00
#!/usr/bin/env python3
2018-11-27 16:21:39 +01:00
import sys
sys.path.append("../ServerCore")
2018-11-27 19:03:38 +01:00
import socket # noqa
2018-12-02 17:08:28 +01:00
import json # noqa
2018-11-27 19:03:38 +01:00
from JSONStreamParser import JSONStreamParser # noqa
2018-12-02 17:08:28 +01:00
import argparse # noqa
2018-11-24 18:04:53 +01:00
2018-12-02 17:07:16 +01:00
def send_request(host, port, datatype, position, data):
2018-11-27 16:21:39 +01:00
parser = JSONStreamParser()
2018-11-24 18:04:53 +01:00
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((host, port))
2018-12-02 17:07:16 +01:00
message_object = {}
if datatype is not None:
message_object['datatype'] = datatype
if position is not None:
message_object['position'] = position
if data is not None:
message_object['data'] = data
s.sendall(json.dumps(message_object).encode())
2018-11-27 16:21:39 +01:00
while parser.isQueueEmpty():
parser.appendRawData(s.recv(4096))
2018-12-02 17:07:16 +01:00
print("received data:\n", parser.popAsJSON())
2018-11-27 16:21:39 +01:00
2018-11-24 18:04:53 +01:00
s.close()
2018-12-02 17:07:16 +01:00
def main():
parser = argparse.ArgumentParser(
description="tester for the voxel Server protocoll")
parser.add_argument('--datatype')
parser.add_argument('--position', nargs=3, type=int)
parser.add_argument('--data')
args = parser.parse_args()
send_request('127.0.0.1', 5050, args.datatype, args.position, args.data)
2018-11-24 18:04:53 +01:00
if __name__ == "__main__":
2018-12-02 17:07:16 +01:00
main()