-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.py
54 lines (46 loc) · 1.68 KB
/
client_test.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
""" Module to Test Sim Listener """
import json
import socket
import sys
import requests
SIM_SERVER_URL = "https://localhost:27464/api/v1/API/register"
def register():
"""Register with Sim Listener"""
content = { 'Content-Type': 'application/json' , 'accept': 'text/plain' }
rdata = { "name": "Python Output Test", "Outputs" : ["PLANE LATITUDE"] }
print( "Registration data =" , json.dumps(rdata) )
try:
req = requests.request( "POST" , SIM_SERVER_URL,
timeout=10,
headers=content,
data=json.dumps(rdata),
verify=False )
except requests.exceptions.ConnectionError :
print( "Server not ready")
sys.exit(1)
print( "RESPONSE = " , req.text )
return json.loads(req.text)
# Set up some data to send
json_data = { "BUTTON POSITION": "ON" }
data = json.dumps(json_data)
# Currently this is running on same server as tcp server.
host = socket.gethostname() # Get local machine name
j = register()
port = int(j['Port'])
#port=9000
print( f"Connecting on {host}:{port}")
# Initialize a TCP client socket using SOCK_STREAM
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_client.connect((host, port))
tcp_client.setblocking( True )
while True:
try:
tcp_client.sendall(data.encode() )
received = tcp_client.recv(1024)
print (f"Bytes Received: {received.decode()}" )
print (f"Bytes Sent: {data}")
except ConnectionResetError as e :
print( f"Socket Closed with {e}" )
sys.exit(1)
except BlockingIOError as e :
pass