-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPServer.py
57 lines (50 loc) · 1.54 KB
/
TCPServer.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
##TCPServer.py
##Dixon Styres
##secuPy
#TCPServer creates a simple client server model with threading and returns to the client
#logs, one line at a time from an input Argus Logfile
from socket import *
import sys
import multiprocessing
import time
import traceback
port = 15015
fileName = ""
# Create welcoming socket using the given port
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind((gethostname(), port))
serverSocket.listen(1)
print 'Listening on port', port, '...'
# While loop to handle clients making requests
def worker():
try:
with open(fileName) as openfileobject:
print("File opened successfully, reading...")
for line in openfileobject:
serverSentence = line
connectionSocket.send(serverSentence)
time.sleep(0.3)
except:
traceback.print_exc()
print("Unable to load file")
print("Usage: python loader.py 'filepath'")
exit()
serverSentence = "QUIT"
connectionSocket.send(serverSentence)
print 'Transfer End'
connectionSocket.close()
return
if __name__ == "__main__":
try:
fileName = str(sys.argv[1])
except:
print("Usage: python server.py 'filename'")
exit()
while 1:
# Waits for some client to connect and creates new socket
# for connection
connectionSocket, addr = serverSocket.accept()
print 'Client Made Connection'
#start a new thread for the client
p = multiprocessing.Process(target=worker)
p.start()