<commit_before>#!/usr/bin/python

"""
Save this file as server.py
>>> python server.py 0.0.0.0 8001
serving on 0.0.0.0:8001

or simply

>>> python server.py
Serving on localhost:8000

You can use this to test GET and POST methods.

"""

import SimpleHTTPServer
import SocketServer
import logging
import cgi
from pprint import pprint
import sys
from os import path
import json


if len(sys.argv) > 2:
    PORT = int(sys.argv[2])
    I = sys.argv[1]
elif len(sys.argv) > 1:
    PORT = int(sys.argv[1])
    I = ""
else:
    PORT = 8000
    I = ""


class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def do_GET(self):
        logging.warning("======= GET STARTED =======")
        logging.warning(self.headers)
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

    def do_POST(self):
        logging.warning("======= POST STARTED =======")
        logging.warning(self.headers)
        length = int(self.headers.getheader('content-length'))
        data = self.rfile.read(length)
        jd = json.loads(data)
        print jd["Connection Details"][0]["hostname"]
        # self.send_response(200, "OK")

        print "#####"
        #self.finish()
        #self.connection.close()

        if self.path == "/pageFour":
            length = int(self.headers.getheader('content-length'))
            postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1)
            print postvars.get ("configuration", "asd")
            for key in postvars:
                print key
                '''
                f = open (filename)
                        contents = f.read ()
                        self.wfile.write (contents)
                        f.close ()
                '''
            SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

        elif self.path == "/download-sample-template":
            # script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
            #file_path = path.relpath()
            # abs_file_path = os.path.join(script_dir, rel_path)
            f = open ("templates/sample.ptpl","r")
            contents = f.read ()
            self.wfile.write (contents) 
            f.close ()
        else:
            # form = cgi.FieldStorage(
            #     fp=self.rfile,
            #     headers=self.headers,
            #     environ={'REQUEST_METHOD':'POST',
            #              'CONTENT_TYPE':self.headers['Content-Type'],
            #              }
            #              )
            logging.warning("======= POST VALUES =======")
            # for item in form.list:
            #     logging.warning(item)
            # logging.warning("\n")
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

Handler = ServerHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "Serving at: http://%(interface)s:%(port)s" % dict(interface=I or "localhost", port=PORT)
httpd.serve_forever()<commit_msg>Sending proper SVG response without Error Texts coming because of GET and no content types<commit_after>#!/usr/bin/python

"""
Save this file as server.py
>>> python server.py 0.0.0.0 8001
serving on 0.0.0.0:8001

or simply

>>> python server.py
Serving on localhost:8000

You can use this to test GET and POST methods.

"""

import SimpleHTTPServer
import SocketServer
import logging
import cgi
from pprint import pprint
import sys
from os import path
import json

sys.path.append("../script")
if len(sys.argv) > 2:
    PORT = int(sys.argv[2])
    I = sys.argv[1]
elif len(sys.argv) > 1:
    PORT = int(sys.argv[1])
    I = ""
else:
    PORT = 8000
    I = ""

class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def do_GET(self):
        logging.warning("======= GET STARTED =======")
        logging.warning(self.headers)
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

    def do_POST(self):
        logging.warning("======= POST STARTED =======")
        logging.warning(self.headers)

        if self.path == "/pageFour":
            length = int(self.headers.getheader('content-length'))
            data = self.rfile.read(length)
            configuration_details = json.loads(data)
            print configuration_details["Connection Details"][0]["hostname"]
            from ptolemy import get_network_flow
            filename = get_network_flow(configuration_details)
            f = open (filename["svg"],"r")
            contents = f.read ()
            self.wfile.write (contents) 
            f.close ()
            return

        elif self.path == "/download-sample-template":
            # script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
            #file_path = path.relpath()
            # abs_file_path = os.path.join(script_dir, rel_path)
            f = open ("templates/sample.ptpl","r")
            contents = f.read ()
            self.wfile.write (contents) 
            f.close ()
        else:
            logging.warning("======= POST VALUES =======")
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

Handler = ServerHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "Serving at: http://%(interface)s:%(port)s" % dict(interface=I or "localhost", port=PORT)
httpd.serve_forever()
{'fraction_non_alphanumeric': 0.13459335624284077, 'fraction_numerical': 0.012218403970981291, 'mean_word_length': 2.274375, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2923229', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1534, 'n_words': 346}