0

I currently have a TCP client code with logging which saves the sent data in a text file. I want the data sent to be saved in a folder which header is the time stamp of the sent data. Is that possible? I have tried using several methods but my code kept failing. Could someone give me a guide , would really be such a helping hand as I have been stuck on this for quite awhile. This is how my code looks like now:

import socket
import thread
import sys

BUFF = 1024 # buffer size
HOST = '172.16.166.206'
PORT = 1234 # Port number for client & server to receive 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
        sys.stdout.write(string)


def handler(clientsock, addr):
    while 1:
        data = clientsock.recv(BUFF) # receive data(buffer).
        logger('data:' + repr(data) + '\n') #Server to receive data sent by client.
        if not data:
            break           #If connection is closed by client, server will break and stop receiving 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(5)
    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))
user3128219
  • 9
  • 1
  • 3
  • It looks like your third attempt to ask [the same question](http://stackoverflow.com/q/20735991/4279). Provide some concrete examples for *"I want the data sent to be saved in a folder which header is the time stamp of the sent data."* (use plain English instead of possibly broken code). – jfs Dec 29 '13 at 22:23
  • It is still unclear: what do you mean by "timestamp" (is it POSIX timestamp as in "seconds since epoch" (a number) or just a string that shows broken-down time in a human-readable form?) – jfs Dec 29 '13 at 22:27
  • Where do you want to put the timestamp (in the folder name, or in the filename, or at the start of each line inside the logfile? How often do you get new timestamp: once per client connection (`handler()` call)?, or every time `.recv()` call returns (it may return 1 byte, it may return all `BUFF` bytes)? (in other words: how do you define message boundaries for input data? What does it mean "sent data" in your case?) – jfs Dec 29 '13 at 22:27

2 Answers2

1

You just need to import time module to generate the time stamp and create the directory named by time stamp.

Suppose you want to cut the log file into different folds by hour. The code will somewhat like bellow:

import time
import os
def logger(string, file_name='logfile.txt', lock=thread.allocate_lock()):
    with lock:
        time_stamp = time.strftime("%Y%m%d%H",time.localtime())
        if not os.path.isdir(time_stamp): os.mkdir(time_stamp)
        with open(os.path.join(time_stamp, file_name), "a") as file_:
            file_.write(string)
            file_.flush()
        sys.stdout.write(string)

The code is not tested.

WKPlus
  • 6,955
  • 2
  • 35
  • 53
0

Check out the Python logging module at:

http://docs.python.org/2/howto/logging.html#logging-basic-tutorial

Jesuisme
  • 1,805
  • 1
  • 31
  • 41