2013-12-19 87 views
0

我需要我的Python TCP客戶端代碼的幫助。我現在有一個基本的TCP客戶端代碼&我需要添加日誌記錄功能,以便我可以在文本文件中看到發送數據的日期時間&。一個用戶友好的基本日誌記錄功能可以。非常感謝您的幫助。對此,我真的非常感激。記錄我的Python TCP服務器

import socket 
import sys 
from thread import * 

HOST = '' # Symbolic name meaning all available interfaces 
PORT = 8888 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
print 'Socket created' 

try: 
s.bind((HOST, PORT)) 
except socket.error , msg: 
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] 
sys.exit() 

print 'Socket bind complete' 

s.listen(10) 
print 'Socket now listening' 

#Function for handling connections 
def clientthread(conn): 
#Sending message to connected client 
conn.send('Welcome to the server. Receving Data...\n') #send only takes string 

#infinite loop so that function do not terminate and thread do not end. 
while True: 

    #Receiving from client 
    data = conn.recv(1024) 
    reply = 'Message Received at the server!\n' 
    print data 
    if not data: 
     break 

    conn.sendall(reply) 

conn.close() 

#now keep talking with the client 



while 1: 
#wait to accept a connection 
conn, addr = s.accept() 
print 'Connected with ' + addr[0] + ':' + str(addr[1])`` 

#start new thread 
start_new_thread(clientthread ,(conn,)) 

s.close() 
+0

你忘了問一個問題。這不是一個寫我的代碼給我的網站。 –

+0

對不起,但你是在錯誤的地方。這裏不是[domyworkforme.com](http://mattgemmell.com/what-have-you-tried/) – FallenAngel

回答