-
Notifications
You must be signed in to change notification settings - Fork 18
/
httpClient.py
executable file
·108 lines (96 loc) · 3.31 KB
/
httpClient.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
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
#!/usr/bin/env python
###########################################################
# Distributed System Assignment 2
# Multithreaded Server and Client
# File Name: tcpserver.py
# Function: Acts as a multithreaded TCP server
# Usage: In terminal "python tcpserver.py" CTRL+C to quit
# Author: Rohail Altaf
###########################################################
# Import necessary libraries
import socket, threading, API
import functionLibrary as fL
from API import API
# threadLock is the global thread lock variable
threadLock = threading.Lock()
API = API()
# The Server object creates a TCP server and listens
# for connections made to it. Once a connection is
# made, it is passed on to the processConnection
# object
class Server():
TCP_ADDRESS = ''
TCP_PORT = 6666
global threadLock
def __init__(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.bind((self.TCP_ADDRESS, self.TCP_PORT))
print "Server Initialzed\n"
def listen(self):
while True:
try:
self.s.listen(1)
self.conn, self.addr = self.s.accept()
self.process = processConnection(self.conn, threadLock)
self.process.start()
except KeyboardInterrupt as e:
print "\nGoodbye"
exit()
# The processConnection object spawns off a thread to
# process the connection when a client connects to the
# server
class processConnection(threading.Thread):
# This makes every thread a daemon thread
daemon = True
global API
# The initializer defines the thread lock and the
# connection details variable
def __init__(self, connection, lock):
threading.Thread.__init__(self)
self.connection = connection
self.lock = lock
# The run function first calls the global variable i
# acquires the thread lock, receives the data from
# the client and returns the same message back along
# with a connection ID (counter value). It then
# increases the counter by one, closes the connection
# and releases the thread lock.
def run(self):
self.lock.acquire()
self.data = fL.recv(self.connection)
dataSplit = self.data.split('|')
print self.data
msg = dataSplit[0]
if(msg == "CREATE"):
create = API.create(dataSplit[1])
if(create == 1):
self.connection.send("CREATE|1")
elif(msg == "APPEND"):
print "append"
append = API.append(dataSplit[1], dataSplit[2], True)
if(append):
self.connection.send("1")
print "1 sent for append"
elif(msg == "READ"):
read = API.read(dataSplit[1], 0, -1, "/var/www/fileshare/download/"+dataSplit[1])
if(read):
self.connection.send("1")
elif(msg == "STREAM"):
stream = API.stream(dataSplit[1], 0, -1, "/var/www/fileshare/download/"+dataSplit[1])
if(stream):
self.connection.send("1")
elif(msg == "FILELIST"):
filelist = API.fileList()
filelist = filelist[1:-1]
split = filelist.split(', ')
string = ""
for i in split:
string += str(i[1:-1]) + "|"
if(filelist):
self.connection.send(string)
#elif(msg == "DELETE"):
self.connection.close()
self.lock.release()
# This initializes the server object and then starts it
server = Server()
server.listen()