-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
51 lines (42 loc) · 1.1 KB
/
server.py
File metadata and controls
51 lines (42 loc) · 1.1 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
import sys
import socket
host = ''
port = 9999 if len(sys.argv) <= 1 else int(sys.argv[1])
backlog = 5
s = None
def socket_create():
try:
global s
s = socket.socket()
except socket.error as msg:
print("Socket Creation Error:", msg)
def socket_bind():
try:
print("Binding socket to port", port)
s.bind((host, port))
s.listen(backlog)
except socket.error as msg:
print("Socket Binding Error:", msg)
socket_bind()
def socket_accept():
conn, address = s.accept()
print("Connection has been established | IP " + address[0] + " | Port " + str(address[1]))
send_commands(conn)
conn.close()
def send_commands(conn):
while True:
cmd = input()
if cmd == 'quit':
conn.close()
s.close()
sys.exit()
if len(cmd) > 0:
conn.send(bytes(cmd, 'utf-8'))
client_reponse = str(conn.recv(1024), 'utf-8')
print(client_reponse, end="")
def main():
socket_create()
socket_bind()
socket_accept()
if __name__ == '__main__':
main()