<commit_before>'''
Created on 19 May 2013

Simple HTTP server to accept images and sensor data to assemble data sets

@author: dsnowdon
'''

import sys
import os
import urlparse
import cgi
import BaseHTTPServer

DATA_DIR=None
PREFIX=None
INDEX = 0

class CollectorRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_POST(self):
        global DATA_DIR
        global PREFIX
        global INDEX
        print "got POST\n"
        ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
        if ctype == 'multipart/form-data':
            postvars = cgi.parse_multipart(self.rfile, pdict)
        elif ctype == 'application/x-www-form-urlencoded':
            length = int(self.headers.getheader('content-length'))
            postvars = urlparse.parse_qs(self.rfile.read(length), keep_blank_values=1)
        else:
            postvars = {}
        print postvars['sensordata']
        filedata = postvars['fileupload']
        if filedata:
            filename = "{prefix}-{index}.jpg".format(prefix=PREFIX, index=INDEX)
            path = os.path.join(DATA_DIR, filename)
            with open(path, "wb") as out:
                out.write(''.join(filedata))
            
        sensordata = postvars['sensordata']
        if sensordata:
            print "Sensordata: {}\n".format(sensordata)
            json_filename = "{prefix}-{index}.json".format(prefix=PREFIX, index=INDEX)
            path = os.path.join(DATA_DIR, json_filename)
            with open(path, "w") as out:
                out.write("{}\n".format(sensordata))

        INDEX = INDEX + 1
        
        self.send_response(200)
        self.send_header("Content-Type", "text/plain")
        self.end_headers()


class CollectorHTTPServer(BaseHTTPServer.HTTPServer):
    def __init__(self, serverAddress, requestClass):
        # HTTPServer is not a new style class<commit_msg>added leading zeros for index in filenames<commit_after>'''
Created on 19 May 2013

Simple HTTP server to accept images and sensor data to assemble data sets

@author: dsnowdon
'''

import sys
import os
import urlparse
import cgi
import BaseHTTPServer

DATA_DIR=None
PREFIX=None
INDEX = 0

class CollectorRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_POST(self):
        global DATA_DIR
        global PREFIX
        global INDEX
        print "got POST\n"
        ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
        if ctype == 'multipart/form-data':
            postvars = cgi.parse_multipart(self.rfile, pdict)
        elif ctype == 'application/x-www-form-urlencoded':
            length = int(self.headers.getheader('content-length'))
            postvars = urlparse.parse_qs(self.rfile.read(length), keep_blank_values=1)
        else:
            postvars = {}
        print postvars['sensordata']
        filedata = postvars['fileupload']
        basename = "{prefix}-{index:04d}".format(prefix=PREFIX, index=INDEX)
        if filedata:
            filename = basename + ".jpg"
            path = os.path.join(DATA_DIR, filename)
            with open(path, "wb") as out:
                out.write(''.join(filedata))
            
        sensordata = postvars['sensordata']
        if sensordata:
            print "Sensordata: {}\n".format(sensordata)
            json_filename = basename + ".json"
            path = os.path.join(DATA_DIR, json_filename)
            with open(path, "w") as out:
                out.write("{}\n".format(sensordata))

        INDEX = INDEX + 1
        
        self.send_response(200)
        self.send_header("Content-Type", "text/plain")
        self.end_headers()


class CollectorHTTPServer(BaseHTTPServer.HTTPServer):
    def __init__(self, serverAddress, requestClass):
        # HTTPServer is not a new style class
{'fraction_non_alphanumeric': 0.10832894043226146, 'fraction_numerical': 0.006852925672113864, 'mean_word_length': 2.5106382978723403, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5380997', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1113, 'n_words': 256}