Python Logging库与套接字通信交互。如何禁用它?

时间:2016-06-23 00:41:16

标签: python sockets logging

我在python中使用简单的客户端和服务器,使用域套接字进行通信。 通信协议如下。首先发送8个字符串长的字符串长度(如果需要,填充0,例如:00000008)然后发送实际字符串(例如,“密码”以上的长度)。

客户端能够成功向服务器发送一些消息,但是当服务器使用sendString发送一些消息时,客户端不会将数字作为第一个字符串接收。它收到了一些与loglog相关的消息。有谁知道如何解决这个问题?

EG。 收到的字符串长度:INFO:roo

具有发送/接收/记录相关代码的命令代码

def setupLogging(suffix):
   logger = logging.getLogger('root')
   FORMAT = "[%(filename)s][%(levelname)s] %(message)s"
   logging.basicConfig(f=FORMAT)
   formatter = logging.Formatter(FORMAT)
   HDLR = logging.FileHandler(LOG_FILE)
   HDLR.setFormatter(formatter)
   logger.addHandler(HDLR)
   logger.setLevel(logging.DEBUG)
   return logger


def sendString(connSock, inputString, logger):
    strLen = len(inputString)
    fStrLen = str(strLen).zfill(8)
    logger.debug("Length of string being sent : " + fStrLen)
    connSock.sendall(fStrLen)
    logger.debug("Data being sent : " + inputString)
    connSock.sendall(inputString)

def receiveString(connSock, logger):
    fStrLen = connSock.recv(8)
    logger.debug("Length of string being received : " + fStrLen)
    strLen = int(fStrLen)
    str = connSock.recv(strLen)
    logger.debug("Data received : " + str)
    return str

服务器示例代码:

serverSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
connection, client_address = serverSocket.accept()
....
        data = receiveString(connection, logger)
        dataStrIO = StringIO.StringIO(data)
        ....
        ....
        sendString(connection, "Dummy", logger)
....

客户端:

 sendString(connSock, "Some string", logger)
    # Problem when parsing first number , at this line int(fStrLen)
    receiveString(connection, logger) 

0 个答案:

没有答案