Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
116 lines (92 loc) · 3.3 KB
/
Copy pathserver.py
File metadata and controls
116 lines (92 loc) · 3.3 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
/* -------------------------------------------------------------
* Authors - Ayush Prakash, Shikhar Agrawal
* Course Project for Computer Networks 2
* This is an implementation of a Load Balancer. The theory and
design are discussed in the report.
* This file contains code for Server Side.
---------------------------------------------------------------
*/
"""
import socket
import threading
import sys
import json
ports = [1234, 1236, 1238, 1240]
# ports = [1234]
def server_program(index):
# create TCP socket
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as msg:
print("Error: could not create socket")
print("Description: " + str(msg))
sys.exit()
# server.bind(('localhost', ports[index]))
try:
# Bind to all interfaces
server.bind(('localhost', ports[index]))
except socket.error as msg:
print("Error: unable to bind on port %d" % ports[index])
print("Description: " + str(msg))
sys.exit()
# server.listen(1)
# Listen
try:
backlog = 1
# Number of incoming connections that can wait
# to be accepted before being turned away
server.listen(backlog)
except socket.error as msg:
print("Error: unable to listen()")
print("Description: " + str(msg))
sys.exit()
print("Listening socket bound to port %d" % ports[index])
print("Server {} online...".format(index+1))
# print "In accept() state"
l, addr = server.accept()
print "Connection to {} established by {}".format(index+1, addr)
while True:
try:
# print "waiting to receive"
info = l.recv(1024)
# print "received data from lb"
client_info = json.loads(info.decode('utf-8'))
data = client_info['data']
client_ip = client_info['ip']
client_port = int(client_info['port'])
request_type = client_info['type']
if data == 'quit_now':
break
# print client_ip, client_port
s_client = socket.socket()
s_client.connect((client_ip, client_port))
# print "connected to client"
if request_type == 'message':
print "Confirmation Message sent to client by server ", index+1, "!"
s_client.send('Message Received!')
# print "message sent to client"
s_client.close()
elif request_type == 'filename':
print "Sending File to client by server ", index+1
f = open(data, 'rb')
temp = f.read(1024)
while(temp):
s_client.send(temp)
temp = f.read(1024)
s_client.send('DONE!')
f.close()
print "File Sent!"
except socket.error as msg:
print("Error: unable to accept()")
print("Description: " + str(msg))
sys.exit()
server.close()
if __name__ == "__main__":
servers = []
for index in range(len(ports)):
servers.append(threading.Thread(target=server_program, args=(index, )))
servers[index].start()
for index in range(len(ports)):
servers[index].join()
print "Servers Closed!"