Repository navigation
Expand file tree
/
Copy pathtcp_server.py
More file actions
64 lines (51 loc) · 1.87 KB
/
Copy pathtcp_server.py
File metadata and controls
64 lines (51 loc) · 1.87 KB
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
import socket
import socketserver
import sys
import time
import json
line=''
class MyTCPHandler(socketserver.BaseRequestHandler):
"""
The request handler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
# self.request is the TCP socket connected to the client
self.data = b""
while True: # needs to always be open
self.data += self.request.recv(1024)
parts = self.data.split(b"\n")
packets = parts[:-1]
self.data = parts[-1]
for packet in packets:
print("{} wrote:".format(self.client_address[0]))
print(packet)
# just send back the same data, but upper-cased
# line = input('input:')
# print(line)
data = {
"MyAction": "buy tomato",
"Recipe": "Boil Water and add salt"
}
send_data = json.dumps(data)
send_data = send_data + "\n"
# line="hahahaha\n"
# self.request.sendall(self.data())
self.request.sendall(send_data.encode())
# self.request.sendall(line.encode())
# self.data = self.request.recv(1024).strip()
# print("{} wrote:".format(self.client_address[0]))
# print(self.data)
if __name__ == "__main__":
HOST, PORT = "localhost", 7778
# Create the server, binding to localhost on port 9999
with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()
print("Connected")
# while True:
# line = input('input:')
# print(line)