-
Notifications
You must be signed in to change notification settings - Fork 4
/
tcpip_util.py
143 lines (117 loc) · 4.16 KB
/
tcpip_util.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'''
Copyright 2024 philippoo66
Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import threading
import socket
import time
import viessdata_util
tcp_client = socket.socket() #None # None, bis der Client-Socket erstellt wird
recdata = bytes()
exit_flag = False
fverbose = True
def run_tcpip(host, port) -> socket:
global exit_flag
global fverbose
# Create a TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the address and port
server_socket.bind((host, port))
# Listen for incoming connections
if(not exit_flag):
print(f"Server listening on {host}:{port}") #if(fverbose):
server_socket.listen(1)
# Wait for a connection
client_socket, client_address = server_socket.accept()
print(f"Connection from {client_address}") #if(fverbose):
# Schließe den Server-Socket, da er nicht mehr benötigt wird
server_socket.close()
return client_socket
def listen_tcpip(client:socket):
global exit_flag
global fverbose
global recdata
if(fverbose): print("enter listen loop")
while(not exit_flag):
try:
data = client.recv(1024)
if data:
if(fverbose): print("TCP recd:", data) #bbbstr(data)
msg = data.decode('utf-8').replace(' ','').replace('\0','').replace('\n','').replace('\r','').replace('"','').replace("'","")
if(msg):
m = msg.lower()
if(m == "exit"):
print("Connection exit")
time.sleep(0.5)
break
elif(m == "flushcsv"):
viessdata_util.buffer_csv_line([], True)
else:
recdata = msg
except ConnectionError:
print("Connection lost")
break
except Exception as e:
print(e)
def tcpip4ever(port:int, verbose = True):
global exit_flag
global tcp_client
global fverbose
# apply verbose flag
fverbose = verbose
# loop buiding connection (if lost) and receiving
while(not exit_flag):
tcp_client.close()
tcp_client = run_tcpip('0.0.0.0', port)
listen_tcpip(tcp_client)
# Schließe den Client-Socket, wenn der Hauptthread beendet wird
if tcp_client:
tcp_client.close()
def get_tcp_request() -> str:
global recdata
ret = recdata
# clear receive buffer
recdata = ''
return ret
def send_tcpip(data):
global tcp_client
global fverbose
# if is string make bytes
if(isinstance(data, str)):
data = bytes(data, 'utf-8')
# send out
tcp_client.send(data)
if(fverbose): print("TCP sent:", data)
def exit_tcpip():
print("exiting TCP/IP client")
global exit_flag
exit_flag = True
# ------------------------
# main for test only
# ------------------------
def main():
global exit_flag
tcp_thread = threading.Thread(target=tcpip4ever, args=(65234,True))
tcp_thread.daemon = True # Setze den Thread als Hintergrundthread. WICHTIG für Ctrl+C etc!
tcp_thread.start()
try:
while(not exit_flag):
mydata = get_tcp_request()
print("###", mydata)
if(mydata):
send_tcpip("received: " + mydata) #.decode('utf-8'))
time.sleep(0.5)
except Exception as e:
print("main", e)
exit_flag = True
tcp_thread.join()
if __name__ == "__main__":
main()