파이썬 TCP 서버 코드에 대한 로깅 파일을 추가하는 데 문제가 있습니다. 몇 가지 예를 살펴 보았지만 자신의 스크립트/코드를 작성하는 데 많은 경험이 없기 때문에이 작업을 수행하는 방법을 잘 모릅니다. 가능하면 누군가가 설명과 함께 올바른 방향으로 나를 안내 할 수 있으면 고맙겠습니다.TCP 서버용 파이썬 로깅
Visual Studio Python 코드가 SERVER 역할을하는 동안 HERCULES SETUP UTILITY를 TCP 클라이언트로 사용하고 있습니다. 내 SERVER는 지금까지 클라이언트가 보낸 데이터를받을 수 있습니다. 텍스트 파일에 저장된 데이터를 저장할 수있는 로깅 파일을 추가 할 수없는 것 같습니다. 누군가 제게 보여 주거나 참조하십시오. 당신의 도움은 많은 것을 의미합니다. 귀하의 질문은 더 나은 고쳐 것 같은
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))
관련 : [라즈베리 파이를위한 파이썬 TCP 서버에 타임 스탬프 파일에 로깅을 추가하는 방법 (http://stackoverflow.com/q/20695241/4279) – jfs