作为测试的一部分,尝试检查插座是否收到了日志 因此,线程发送日志,主线程尝试在套接字处检索。
无法将收到的数据转换为日志记录
根据{{3}}尝试logging.makeLogRecord
。还尝试了pickle
/ cPickle
import unittest
import socketLogger
import logging
import logging.handlers
#import cPickle as pickle
def test_StringReceivedIsSameAsStringSent():
host = 'localhost'
port = 9000
stringSent = "hello world!"
stringReceived = None
log_msg = None
def sendLogToSocket(host,port, stringSent):
logger = logging.getLogger('mylogger') # to log Led Observer output over a socket
sh = logging.handlers.SocketHandler(host,port) # handler to write to socket
logger.addHandler(sh)
logger.critical(stringSent)
logger.removeHandler(sh)
sh.close()
import threading
t = threading.Thread(target=sendLogToSocket, args=(host,port,stringSent)) # socket requires 2 different ports if on the same machine
t.start() # send log in a thread
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #INET => IPv4, STREAM => TCP
serversocket.bind((host,port)) # 'localhost' => implies only visible within the same machine
serversocket.listen(1) # accept 1 connection only
(clientsocket, address) = serversocket.accept()
stringReceived = clientsocket.recv(1024)
print 'socketlistener: string received: ', repr(stringReceived)
#obj = pickle.loads(stringReceived)
#print 'un pickling log: ', obj
#log_msg = logging.makeLogRecord(obj)
log_msg = logging.makeLogRecord(stringReceived)
print 'socketlistener: converted to log: ', log_msg
clientsocket.close()
serversocket.close()
t.join() # wait for the log thread to finish
print 'string sent: ', repr(stringSent), ' received: ', repr(stringReceived
self.assertEqual(stringSent, stringReceived)
if __name__ == "__main__":
test_StringReceivedIsSameAsStringSent()
输出
E:\> python testSocket.py
socketlistener: string received: '\x00\x00\x01}}q\x01(U\x0frelativeCreatedq\x02 G@\x18\x00\x0b\x00\x00\x00\x00U\x07processq\x03M\xb0IU\x04argsq\x04NU\x06moduleq \x05U\ntestSocketq\x06U\x08funcNameq\x07U\x0fsendLogToSocketq\x08U\x08exc_textq\ tNU\x04nameq\nU\x08myloggerq\x0bU\x06threadq\x0cM|"U\x07createdq\rGA\xd6a\x040\x b1x\xd5U\nthreadNameq\x0eU\x08Thread-1q\x0fU\x05msecsq\x10G@\x88(\x00\x01\x00\x0 0\x00U\x08filenameq\x11U\rtestSocket.pyq\x12U\x07levelnoq\x13K2U\x0bprocessNameq \x14U\x0bMainProcessq\x15U\x08pathnameq\x16h\x12U\x06linenoq\x17K\x12U\x03msgq\x 18U\x0chello world!q\x19U\x08exc_infoq\x1aNU\tlevelnameq\x1bU\x08CRITICALq\x1cu.
'
Traceback (most recent call last):
File "testSocket.py", line 47, in <module>
test_StringReceivedIsSameAsStringSent()
File "testSocket.py", line 36, in test_StringReceivedIsSameAsStringSent
log_msg = logging.makeLogRecord(stringReceived)
File "C:\Users\myuser\AppData\Local\Continuum\Miniconda2\lib\logging\__init_ _.py", line 340, in makeLogRecord
rv.__dict__.update(dict)
ValueError: dictionary update sequence element #0 has length 1; 2 is required
答案 0 :(得分:0)
要求1.解压缩大小和数据,2。使用size和3. unpmarkle数据取消对未打开对象的makeLogRecord(之前错过的步骤1)
感谢python docs上的代码:logging cookbook sample和network logging sample
这里我的代码已更正(假设我们在一个socket.receive调用中得到大小和完整的消息):
import unittest
import socketLogger
import logging
import logging.handlers
import pickle
#import cPickle as pickle
def test_StringReceivedIsSameAsStringSent():
host = 'localhost'
port = 9000
stringSent = "hello world!"
stringReceived = None
log_msg = None
def sendLogToSocket(host,port, stringSent):
logger = logging.getLogger('mylogger') # to log Led Observer output over a socket
sh = logging.handlers.SocketHandler(host,port) # handler to write to socket
logger.addHandler(sh)
logger.critical(stringSent)
logger.removeHandler(sh)
sh.close()
import threading
t = threading.Thread(target=sendLogToSocket, args=(host,port,stringSent)) # socket requires 2 different ports if on the same machine
t.start() # send log in a thread
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #INET => IPv4, STREAM => TCP
serversocket.bind((host,port)) # 'localhost' => implies only visible within the same machine
serversocket.listen(1) # accept 1 connection only
(clientsocket, address) = serversocket.accept()
chunk = clientsocket.recv(1024)
print 'socketlistener: data received: ', repr(chunk)
import struct
slen = struct.unpack(">L", chunk[:4])[0]
obj = pickle.loads(chunk[4:])
print 'un pickling log: ', repr(obj)
stringReceived = logging.makeLogRecord(obj)
#log_msg = logging.makeLogRecord(stringReceived)
print 'socketlistener: converted to log: ', repr(stringReceived)
clientsocket.close()
serversocket.close()
t.join() # wait for the log thread to finish
print 'string sent: ', repr(stringSent), ' received: ', repr(stringReceived.getMessage())
assert(stringSent == stringReceived.getMessage())
if __name__ == "__main__":
test_StringReceivedIsSameAsStringSent()
输出
E:\> python testSocket.py
socketlistener: data received: '\x00\x00\x01}}q\x01(U\x0frelativeCreatedq\x02G@ !\xff\xe9\x00\x00\x00\x00U\x07processq\x03M@5U\x04argsq\x04NU\x06moduleq\x05U\nt estSocketq\x06U\x08funcNameq\x07U\x0fsendLogToSocketq\x08U\x08exc_textq\tNU\x04n ameq\nU\x08myloggerq\x0bU\x06threadq\x0cM\\JU\x07createdq\rGA\xd6a\x05\x87\xbfl\ x8bU\nthreadNameq\x0eU\x08Thread-1q\x0fU\x05msecsq\x10G@\x8e\xf7\xff\xdf\x00\x00 \x00U\x08filenameq\x11U\rtestSocket.pyq\x12U\x07levelnoq\x13K2U\x0bprocessNameq\ x14U\x0bMainProcessq\x15U\x08pathnameq\x16h\x12U\x06linenoq\x17K\x13U\x03msgq\x1 8U\x0chello world!q\x19U\x08exc_infoq\x1aNU\tlevelnameq\x1bU\x08CRITICALq\x1cu.'
un pickling log: {'threadName': 'Thread-1', 'name': 'mylogger', 'thread': 19036 , 'relativeCreated': 8.999824523925781, 'process': 13632, 'args': None, 'module' : 'testSocket', 'funcName': 'sendLogToSocket', 'levelno': 50, 'processName': 'Ma inProcess', 'created': 1501828638.991, 'msecs': 990.9999370574951, 'msg': 'hello world!', 'exc_info': None, 'exc_text': None, 'pathname': 'testSocket.py', 'file name': 'testSocket.py', 'levelname': 'CRITICAL', 'lineno': 19}
socketlistener: converted to log: <logging.LogRecord object at 0x000000000276F4
00>
string sent: 'hello world!' received: 'hello world!'