change
This commit is contained in:
parent
262940bb44
commit
59c9461594
38
server/yadyn
38
server/yadyn
@ -3,8 +3,11 @@
|
||||
import select
|
||||
import socket
|
||||
import time
|
||||
import random
|
||||
|
||||
|
||||
ALPHANUM = (0,1,2,3,4,5,6,7,8,9,
|
||||
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')
|
||||
|
||||
class DoneException(Exception): pass
|
||||
|
||||
@ -16,13 +19,40 @@ class Client(object):
|
||||
self.address = address
|
||||
self.timestamp = int(time.time())
|
||||
self.timeout = 5
|
||||
self.state = 0
|
||||
|
||||
def process(self):
|
||||
self.timestamp = int(time.time())
|
||||
data = self.sock.recv(8192)
|
||||
if data == None or data == '':
|
||||
raise DoneException
|
||||
print data
|
||||
if data[-1] in ('\r','\n'):
|
||||
data = data[:-1]
|
||||
if data[-1] in ('\r','\n'):
|
||||
data = data[:-1]
|
||||
print "<%s> %d" % (data, len(data))
|
||||
|
||||
|
||||
if self.state == 0:
|
||||
#print "State 0"
|
||||
if data == "hello":
|
||||
self.challenge = ''.join(random.sample(ALPHANUM, 5))
|
||||
print self.challenge
|
||||
self.sock.send(self.challenge)
|
||||
self.state = 1
|
||||
return
|
||||
else:
|
||||
raise DoneException
|
||||
elif self.state == 1:
|
||||
print "State 1"
|
||||
if data == self.challenge:
|
||||
self.sock.send("OK")
|
||||
raise DoneException
|
||||
|
||||
print "After state machine"
|
||||
|
||||
|
||||
|
||||
|
||||
def checkTimeout(self):
|
||||
if self.timestamp + self.timeout < int(time.time()):
|
||||
@ -32,6 +62,8 @@ class Client(object):
|
||||
self.sock.close()
|
||||
|
||||
|
||||
|
||||
|
||||
sSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
sSock.bind(("", 9090))
|
||||
@ -61,8 +93,8 @@ while True:
|
||||
client = clients[fd]
|
||||
try:
|
||||
client.process()
|
||||
except:
|
||||
# print "Closing %d" % fd
|
||||
except Exception, e:
|
||||
print "Closing %d, %s" % (fd, str(e))
|
||||
client.close()
|
||||
del clients[fd]
|
||||
p.unregister(fd)
|
||||
|
Loading…
x
Reference in New Issue
Block a user