def wrapper(*args, **kwargs): start_time = time.time() func(*args, **kwargs) end_time = time.time() time_log = 'method <'+ func.__name__ + '> use time:'+ \ str(end_time-start_time) + 's' log.info(time_log)
def upload(): if request.method == 'POST': f = request.files['file'] basepath = os.path.dirname(__file__) # 当前文件所在路径 upload_path = os.path.join(basepath, 'static\uploads', secure_filename(f.filename)) # upload_path = os.path.join('C:\Users\hp\Documents\Neo4j\default.graphdb\import') log.info('go+++++++++++++++++++++++:%s' % upload_path) # upload_path = os.path.join(basepath) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径 try: f.save(upload_path) except Exception, e: log.error('write the msg into file error, reason is: %s' % e) return '写入失败,因为:%s' % e return redirect(url_for('upload'))
def begin(self): print('WebSocketServer Start!') self.socket = socket(AF_INET, SOCK_STREAM) self.socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) self.socket.bind(("0.0.0.0", 4567)) self.socket.listen(50) global connectionlist i = 0 while True: connection, address = self.socket.accept() log.info('the new socket is: %s, and link to the client address is: %s' % (connection, address)) username = address[0] newSocket = WebSocket(connection, i, username, address) newSocket.start() # 开始线程,执行run函数 connectionlist['connection'+str(i)] = connection i += 1
def broadcast_message(message): global connectionlist message_utf_8 = message.encode('utf-8') log.info('connectlist array is: %s' % connectionlist) for connection in connectionlist.values(): back_str = [] back_str.append('\x81') data_length = len(message_utf_8) if data_length <= 125: back_str.append(chr(data_length)) elif data_length <= 65535: back_str.append(struct.pack('b', 126)) back_str.append(struct.pack('>h', data_length)) # back_str.append(chr(data_length >> 8)) # back_str.append(chr(data_length & 0xFF)) # a = struct.pack('>h', data_length) # b = chr(data_length >> 8) # c = chr(data_length & 0xFF) elif data_length <= (2 ^ 64 - 1): # back_str.append(chr(127)) back_str.append(struct.pack('b', 127)) back_str.append(struct.pack('>q', data_length)) # back_str.append(chr(data_length >> 8)) # back_str.append(chr(data_length & 0xFF)) else: print('太长了') msg = '' for c in back_str: msg += c back_str = str(msg) + message_utf_8 # encode('utf-8') # connection.send(str.encode(str(u"\x00%s\xFF\n\n" % message))) #这个是旧版 # print (u'send message:' + message) if back_str is not None and len(back_str) > 0: print(back_str) try: connection.send(back_str) except Exception, e: log.error('send error, reason is: %s' % e)
# -*- coding: utf-8 -*- # Author: bill-jack<*****@*****.**> from logs import logging as log from py2neo import Graph from igraph import Graph as IGraph graph = Graph("http://localhost:7474", username="******", password="******") query = ''' MATCH (c1:person)-[r:rel]->(c2:person) RETURN c1.name, c2.name, r.property1 AS weight ''' ig = IGraph.TupleList(graph.run(query), weights=True) # 社区发现 if __name__ == '__main__': try: log.info('ig is: type(%s)' % type(ig)) clusters = IGraph.community_walktrap(ig, weights=None).as_clustering() except Exception, e: log.error('error.....:%s' % e) raise nodes = [{"name": node["name"]} for node in ig.vs] for node in nodes: idx = ig.vs.find(name=node["name"]).index node["community"] = clusters.membership[idx] write_clusters_query = ''' UNWIND {nodes} AS n MATCH (c:person) WHERE c.name = n.name SET c.community = toInt(n.community) ''' graph.run(write_clusters_query, nodes=nodes) # 在Neo4j中查询有多少个社区以及每个社区的成员数:
def run(self): # 重载Thread的run print('Socket%s Start!' % self.index) headers = {} self.handshaken = False while True: if self.handshaken is False: # 握手 log.info('Socket%s Start Handshaken with %s!' % (self.index, self.remote)) self.buffer += bytes.decode(self.conn.recv(1024)) log.info('buffer data is: %s' % self.buffer) if self.buffer.find('\r\n\r\n') != -1: header, data = self.buffer.split('\r\n\r\n', 1) for line in header.split("\r\n")[1:]: key, value = line.split(": ", 1) headers[key] = value headers["Location"] = ("ws://%s%s" % (headers["Host"], self.path)) key = headers['Sec-WebSocket-Key'] token = b64encode( hashlib.sha1(str.encode(str(key + self.GUID))).digest()) handshake = "HTTP/1.1 101 Switching Protocols\r\n"\ "Upgrade: websocket\r\n"\ "Connection: Upgrade\r\n"\ "Sec-WebSocket-Accept: "+bytes.decode(token)+"\r\n"\ "WebSocket-Origin: "+str(headers["Origin"])+"\r\n"\ "WebSocket-Location: "+str(headers["Location"])+"\r\n\r\n" log.info('handshake is: %s' % handshake) self.conn.send(str.encode(str(handshake))) self.handshaken = True log.info('Socket %s Handshaken with %s success!' % (self.index, self.remote)) # send_message(self.conn, 'Welcome, ' + self.name + ' !') # sendMessage(socket_client(self.name)) send_message(self.conn, 'connect the server successed!') self.buffer_utf8 = "" g_code_length = 0 else: global g_code_length global g_header_length mm = self.conn.recv(1024) if len(mm) <= 0: continue if g_code_length == 0: get_datalength(mm) # 接受的长度 self.length_buffer = self.length_buffer + len(mm) self.buffer = self.buffer + mm if self.length_buffer - g_header_length < g_code_length: continue else: self.buffer_utf8 = parse_data(self.buffer) # utf8 msg_unicode = str(self.buffer_utf8).decode( 'utf-8', 'ignore') # unicode if msg_unicode == 'quit': print('Socket%s Logout!' % self.index) nowTime = time.strftime('%H:%M:%S', time.localtime(time.time())) # send_message('%s %s say: %s' % (nowTime, self.remote, self.name+' Logout')) send_message(self.conn, socket_client('Logout')) deleteconnection(str(self.index)) self.conn.close() break # 退出线程 else: # print (u'Socket%s Got msg:%s from %s!' % (self.index, msg_unicode, self.remote)) nowTime = time.strftime(u'%H:%M:%S', time.localtime(time.time())) # send_message(self.conn, u'%s %s say: %s' % (nowTime, self.remote, msg_unicode)) # if msg_unicode != 'lpa' and msg_unicode != 'fast_unfolding': # send_message(self.conn, 'parameters error') # break try: if msg_unicode == 'lpa': test(self.conn) self.conn.close() break if msg_unicode == 'fast': unfold(self.conn) self.conn.close() break except Exception, e: print "error..., reason is: %s" % e self.conn.close() break # finally: # self.conn.close() # send_message(self.conn, socket_client(msg_unicode)) # 重置buffer和bufferlength self.buffer_utf8 = "" self.buffer = "" g_code_length = 0 self.length_buffer = 0 self.buffer = ""
def socket_client(data): client = socket(AF_INET, SOCK_STREAM) client.connect(ADDR) try: client.send(data.encode('utf8')) except Exception, e: log.error("send message error, reason is : %s" % e) try: data = client.recv(BUFSIZ) except Exception, e: log.error("recv the message error, reason is: %s" % e) log.info(data.decode('utf8')) return data # 广播用 def broadcast_message(message): global connectionlist message_utf_8 = message.encode('utf-8') log.info('connectlist array is: %s' % connectionlist) for connection in connectionlist.values(): back_str = [] back_str.append('\x81') data_length = len(message_utf_8) if data_length <= 125: