class WSGIServer(object): def __init__(self, host, port): """init""" # create a socket object, and declare the type self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # set host and port self.server.bind((host, port)) # start to listen, and there can be 30 connections at most in queue self.server.listen(30) # instance a Motor object self.motor = Motor() self.num = 0 def get_data(self, conn, addr): """receive data and insert data into database""" while True: try: length = struct.unpack( 'i', conn.recv(4)) # rceive the length of data data = conn.recv(length[0]) # Translate bytes data into dictionary data_dict = json.loads(data.decode()) # insert data into database self.motor.insert_data(data_dict) # check print('recive:', data.decode()) # decode the data and print # conn.send(data.upper()) # send data back except: if self.num < 20: self.num += 1 print('data error') else: print('connection over') self.num = 0 conn.close() return def run(self): """get connected""" while True: conn, addr = self.server.accept() print("connected with: {}".format(addr)) # when a new socket is connected, start a new thread t1 = threading.Thread(target=self.get_data, args=(conn, addr)) t1.start()
def __init__(self, host, port): """init""" # create a socket object, and declare the type self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # set host and port self.server.bind((host, port)) # start to listen, and there can be 30 connections at most in queue self.server.listen(30) # instance a Motor object self.motor = Motor() self.num = 0
class MyServer(socketserver.BaseRequestHandler): """ 必须继承socketserver.BaseRequestHandler类 """ def handle(self): """ 必须实现这个方法! :return: """ self.motor = Motor() conn = self.request # request里封装了所有请求的数据 conn.sendall('欢迎访问socketserver服务器!'.encode()) t = time.localtime() logging.info("At time {} connected with: {}".format(time.asctime(t), self.client_address)) while True: try: #length = struct.unpack('i', conn.recv(4)) # rceive the length of data data_all = b'' #l = length[0] l = 23948 # Sometimes data has a stable length # get all of data while l > 0: data = conn.recv(l) data_all += data l -= len(data) # Translate bytes data into dictionary data_dict = json.loads(data_all.decode('utf-8')) # insert data into database self.motor.insert_data(data_dict) logging.info('Inserted scuuessfully') except: logging.error('data error') logging.info('connection over') self.num = 0 conn.close() return
def handle_stream(self, stream, address): """ Args: stream: address: """ t = time.localtime() logging.info("Socket served at time: {} with: {}".format( time.asctime(t), address)) DataServer.clients[address] = stream while True: try: read_l = yield stream.read_bytes(4, partial=True) length = struct.unpack('i', read_l) # print(length) logging.info("successfully connected ...") data_all = b'' # l = 23948 l = length[0] while l > 0: data = yield stream.read_bytes(l, partial=True) data_all += data l -= len(data) try: data_dict = json.loads(data_all.decode()) motor = Motor() rh = RedisHandler() # Insert data into redis and mysql # rh.inhash(data_dict.copy()) motor.insert_data(data_dict) logging.info('Inserted successfully') except KeyError: logging.error('** Data Error **') except StreamClosedError: logging.info("Server finished") logging.info("*" * 25) del DataServer.clients[address] break
class WSGIServer(object): def __init__(self, host, port): """init""" # create a socket object, and declare the type self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # set host and port self.server.bind((host, port)) # start to listen, and there can be 30 connections at most in queue self.server.listen(30) # instance a Motor object self.motor = Motor() self.num = 0 self.jud = True def get_data(self, conn, addr): """receive data and insert data into database""" while self.jud: print('here') # try: length = struct.unpack('i', conn.recv(4)) # rceive the length of data data_all = b'' print(length) l = length[0] # l = length[0] # l = 23948 # Sometimes data has a stable length # get all of data while l > 0: data = conn.recv(l) data_all += data l -= len(data) # Translate bytes data into dictionary data_dict = json.loads(data_all.decode('utf-8')) # print(data_dict) # insert data into database self.motor.insert_data(data_dict) #print('Inserted scuuessfully') ''' except: print('data error') #print('connection over') logging.error('data error') logging.warning('connecting over') self.num = 0 conn.close() #return self.jud = False ''' def run(self): """get connected""" print('run') while True: conn, addr = self.server.accept() #print("connected with: {}".format(addr)) t = time.localtime() # when a new socket is connected, start a new thread t1 = threading.Thread(target=self.get_data, args=(conn, addr), daemon=True) t1.start()