-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·51 lines (37 loc) · 1.15 KB
/
server.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
#!/usr/bin/python
import re
import socket
import sys
import common
from subprocess import check_output
sendPattern = re.compile( r"SEND\s*(\d+)\n", re.IGNORECASE )
quitPattern = re.compile( r"QUIT\n" )
def isQuitCommand( message ):
return not quitPattern.match( message ) is None
def isSendCommand( message ):
return not sendPattern.match( message ) is None
def parseBytesRequested( sendCommand ):
return int( sendPattern.match( sendCommand ).group( 1 ) )
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
print "server running"
if len( sys.argv ) > 1:
TCP_IP, TCP_PORT = common.readIP( sys.argv[1] )
#socket.setdefaulttimeout( 0 )
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
print "listening..."
connection, _ = s.accept()
print "connection accepted"
filestream = connection.makefile( "r" )
data = ""
while True:
data = filestream.readline()
if isQuitCommand( data ):
break
elif isSendCommand( data ):
connection.sendall( "a" * parseBytesRequested( data ) )
# Remove the send command from the head of the accumulated data.
data = re.sub( r"^[^\n]*\n", "", data )
connection.close()