python logger 服务器_TCP服务器的Python日志记录

I am having some problems adding in a logging file for my python TCP server code.

I've looked at some examples, but as I don't have much experience in writing my own scripts/codes, I'm not very sure how to go about doing this. I would appreciate if someone could guide me in the right direction with explanation and some examples if possible.

I am using HERCULES SETUP UTILITY , which acts as my TCP client, while my visual studio python code acts as a SERVER. My SERVER can receive the data which is sent by the client by now , I just can't seem to add in a logging file which can save the sent data into text file.Can someone please show me some examples or referance please? Your help would mean alot. This is my code so far :

from socket import *

import thread

BUFF = 1024 # buffer size

HOST = '172.16.166.206'# IP address of host

PORT = 1234 # Port number for client & server to recieve data

def response(key):

return 'Sent by client'

def handler(clientsock,addr):

while 1:

data = clientsock.recv(BUFF) # receive data(buffer).

print 'data:' + repr(data) #Server to recieve data sent by client.

if not data: break #If connection is closed by client, server will break and stop recieving data.

print 'sent:' + repr(response('')) # respond by saying "Sent By Client".

if __name__=='__main__':

ADDR = (HOST, PORT) #Define Addr

serversock = socket(AF_INET, SOCK_STREAM)

serversock.bind(ADDR) #Binds the ServerSocket to a specific address (IP address and port number)

serversock.listen(0)

while 1:

print 'waiting for connection...'

clientsock, addr = serversock.accept()

print '...connected from:', addr #show its connected to which addr

thread.start_new_thread(handler, (clientsock, addr ))

解决方案

In context, maybe something like this?

#!/usr/local/cpython-2.7/bin/python

import socket

import thread

BUFF = 1024 # buffer size

HOST = '127.0.0.1'

PORT = 1234 # Port number for client & server to recieve data

def response(key):

return 'Sent by client'

def logger(string, file_=open('logfile.txt', 'a'), lock=thread.allocate_lock()):

with lock:

file_.write(string)

file_.flush() # optional, makes data show up in the logfile more quickly, but is slower

def handler(clientsock, addr):

while 1:

data = clientsock.recv(BUFF) # receive data(buffer).

logger('data:' + repr(data) + '\n') #Server to recieve data sent by client.

if not data:

break #If connection is closed by client, server will break and stop recieving data.

logger('sent:' + repr(response('')) + '\n') # respond by saying "Sent By Client".

if __name__=='__main__':

ADDR = (HOST, PORT) #Define Addr

serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

serversock.bind(ADDR) #Binds the ServerSocket to a specific address (IP address and port number)

serversock.listen(0)

while 1:

logger('waiting for connection...\n')

clientsock, addr = serversock.accept()

logger('...connected from: ' + str(addr) + '\n') #show its connected to which addr

thread.start_new_thread(handler, (clientsock, addr))

HTH

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值