Skip to content

Commit

Permalink
测试,修改一些bug;
Browse files Browse the repository at this point in the history
  • Loading branch information
fan committed Apr 24, 2017
1 parent 2448aef commit ee681fe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
31 changes: 9 additions & 22 deletions Server/Client.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
#!/usr/bin/python
#coding=UTF-8

# import time

# if __name__ == '__main__':
# import socket
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# sock.connect(('localhost', 8001))
# import time
# time.sleep(2)
# for i in range(1000):
# sock.send(str(i))
# time.sleep(1)
# # print sock.recv(1024)
# sock.close()

import socket
import json
import time
import random
from threading import Thread
# from decimal import Decimal
import codecs

from DataUtils import Protocol

words = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'

# Message = 'head:aaa:0001/0010:3:{'aaa': 1}:end'

class Sender(Thread):
"""docstring for Sender"""
def __init__(self, host, port, dataObj, dataType):
super(Sender, self).__init__()
self.amount = 512
Expand All @@ -42,23 +25,27 @@ def __init__(self, host, port, dataObj, dataType):
def getPId(self):
return ''.join(random.sample(words, 5)) + '_' + str(int(time.time() * 1000))

def makeMessage(self, obj, dataType):
def makeMessage(self, data, dataType):
pId = self.getPId()
messageList = list()
data = json.dumps(object2dict(obj))
size = len(data)
sNum = (size / self.amount) + 1
for i in xrange(0, sNum):
s = i * self.amount
e = (i + 1) * self.amount
p = Protocol(pId = self.getPId(), sId = i + 1, sNum = sNum, size = size, data = data[s:e], dataType = dataType)
p = Protocol(pId = pId, sId = i + 1, sNum = sNum, size = size, data = data[s:e], dataType = dataType)
messageList.append(str(p))
return messageList

def run(self):
messageList = self.makeMessage(self.dataObj, self.dataType)
self.socket.connect((self.host, self.port))
for msg in messageList:
self.socket.send
while True:
self.socket.send(msg)
ret = self.socket.recv(1024)
if ret == 'ok':
break
self.socket.close()

def object2dict(obj):
Expand Down
2 changes: 1 addition & 1 deletion Server/DataUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import re

pProtocol = re.compile(r'^head:(\w+?):(\w+?):(\d+?)/(\d+?):(\d+?):(.+?):end$')
pProtocol = re.compile(r'^head:(\w+?):(\w+?):(\d+?)/(\d+?):(\d+?):([\s|\S]+?):end$')

class Protocol(object):
def __init__(self, pId = '', sId = 0, sNum = 0, size = 0, data = '', dataType = 'text'):
Expand Down
26 changes: 19 additions & 7 deletions Server/Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
# self.data = data

class Listener(Thread):
def __init__(self, connection, address, bufferSize = 1024):
def __init__(self, connection, address, callback, bufferSize = 1024):
super(Listener, self).__init__()
self.connection = connection
self.address = address
self.bufferSize = bufferSize
self.dataDict = dict()
self.callback = callback

'''head:aaa:1/10:3:{'aaa': 1}:end'''
def analysis(self, data):
Expand All @@ -49,15 +50,23 @@ def analysis(self, data):
if size == len(retP.data):
del self.dataDict[pId]
retP.data = json.loads(retP.data)
return retP
return None
return retP, 'ok'
return None, 'ok'
else:
return None, 'error'

def run(self):
while True:
buf = self.connection.recv(self.bufferSize)
p = self.analysis(buf)
p, ret = self.analysis(buf)
self.connection.send(ret)
if p is not None:
print p.data
# f = open('%s.txt'%p.pId, 'w')
# f.write(p.data)
# f.close()
# print 'over'
self.callback(self)
break
self.connection.close()

class Server(object):
Expand All @@ -70,7 +79,7 @@ def __init__(self, connectionType = 'long'):
self.addressDict = dict()

self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind(('localhost', 8001))
self.socket.bind(('localhost', 8008))
self.socket.listen(self.num)

def close(self):
Expand All @@ -86,12 +95,15 @@ def closeServer(self, address):
if address in self.addressDict:
del self.addressDict[address]

def callback(self, obj):
pass

def run(self):
while not self.close:
connection, address = self.socket.accept()
connection.settimeout(self.timeout)
if address not in self.addressDict:
self.addressDict[address] = Listener(connection, address)
self.addressDict[address] = Listener(connection, address, self.callback)
self.addressDict[address].start()
print self.addressDict
# self.listen(connection)
Expand Down

0 comments on commit ee681fe

Please sign in to comment.