<commit_before>#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
'''
Simple web server that demonstrates how browser/server interactions
work for HEAD, GET and POST requests using HTTP or HTTPS. It can be
customized using plugins.

This is an all-in-one solution. You do not need to separate out your
server from your code and you do not need a web server gateway
interface (WSGI). Just run this single program with a custom plugin.

It is not replacement for production systems. It is more of a
demonstration project that shows how all of the pieces fit together.

You can use it as a starting point to create a custom web server for
handling specific requests or for improving your understanding of how
web servers work but don't try to use it for any production work
unless it is for a simple, low traffic site.

It is very easy to use, simply run it and it will start serving
content from the current directory.

   $ ./webserver.py

This will start up an HTTP server listening on port 8080 for the local
host using the current directory as the root of the web directory
handling requests with the built in default request handler.

You can then view the output using favorite browser:

   $ firefox localhost:8080

See the specific examples in the help below for more usage details.

One interesting feature of this system is that is allows embedded
python in HTML modules. This allows dynamic web pages to be created.

Here is a very simple example:

    <!DOCTYPE HTML>
    <!-- python
      params = locals()
      params['title'] = 'Title of Page'
      params['arg1'] = 'foo'
      params['arg2'] = 42
    -->
    <html>
      <head>
        <meta charset="utf-8">
        <title>{title}</title>
      </head>
      <body>
        <pre>
         arg1 = {arg1}
         arg2 = {arg2}
        </pre>
      </body>
    </html>

As you can see, the python code defines the values of variables that
used in the HTML.

The format of the variables is the same that used for string.format()
operations (string.Formatter objects) so they are very flexible.

The embedded python can also reference other files. Here is an
example of how that works:

    <!DOCTYPE html>
    <!-- python
      # Define the variables used on the page using
      # the full power of python.
      # The params dictionary is defined by the
      # request handler.
      import datetime
      import os
      import sys

      def read_file(params, ifn):
        """
        Read a file.
        """
        try:
          ifn = os.path.join(params['sysdir'], ifn)
          with open(ifn, 'r') as ifp:
            return ifp.read()
        except IOError as exc:
          return 'Read failed for {0}: {1!r}.'.format(ifn, exc)

      params = locals()

      params['page_header'] = read_file(params, 'page_header.html')
      params['page_footer'] = read_file(params, 'page_footer.html')
      params['title'] = 'Template Test of Embedded Python'

      # This is referenced by the page_header after the substitution.
      params['date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

      params['python_version'] = 'Python {0}.{1}.{2}'.format(sys.version_info[0],
                                                             sys.version_info[1],
                                                             sys.version_info[2])
      data = ''
      for i in range(5):
         data += '   {0} Count {0}\n'.format(i)
      params['data'] = data[:-1]  # strip the last new line

      params['top'] = '<a href="/">Top</a>'
    -->
    <html>
      <head>
        <meta charset="utf-8">
        <title>Template Test</title>
        <link rel="icon" type="image/png" href="/webserver.png">
        <link href="/webserver.css" rel="stylesheet">
        <script src="/webserver.js"></script>
      </head>
      <body>
        <!-- page header (references {title} and {date}) -->
        {page_header}

        <!-- page body -->
        <pre>
    {python_version}

    Loop Data
    {data}
        </pre>
    {top}
        <!-- page footer -->
        {page_footer}
      </body>
    </html>

The python code will be left justified automatically but other than
that it must have proper indenting.
'''
from __future__ import print_function

# Compatibility testing.
import sys
pver = 'Python {0}.{1}.{2}'.format(sys.version_info[0],
                                   sys.version_info[1],
                                   sys.version_info[2])
if sys.version_info[0] == 3:
    # There are some known problems with
    # byte/string conversions.
    import http.server as HTTPServer
    sys.exit('Sorry, {0} is not supported.'.format(pver))
elif sys.version_info[0] == 2:
    if sys.version_info[0] == 2 and sys.version_info[1] < 7:
        sys.exit('Sorry, {0} is not supported. You must use Python 2.7 or later.'.format(pver))
    import SimpleHTTPServer as HTTPServer
else:
    sys.exit('Sorry, {0} is not supported.'.format(pver))
del pver

# Standard imports.
import argparse
import cgi
import Cookie
import imp
import logging
import logging.handlers
import mimetypes
import random
import re
import os
import socket
import SocketServer
import ssl
import string
import subprocess


VERSION = '1.0'


def logger_init(opts, name, level=logging.INFO):
    '''
    Create a basic logger with a stdout stream handler.
    '''
    logger = logging.getLogger(name)
    handler = logging.StreamHandler(stream=sys.stdout)
    formatter = logging.Formatter(opts.log_format)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(level)
    return logger


def logger_update(opts, logger):
    '''<commit_msg>Added missing select import<commit_after>#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
'''
Simple web server that demonstrates how browser/server interactions
work for HEAD, GET and POST requests using HTTP or HTTPS. It can be
customized using plugins.

This is an all-in-one solution. You do not need to separate out your
server from your code and you do not need a web server gateway
interface (WSGI). Just run this single program with a custom plugin.

It is not replacement for production systems. It is more of a
demonstration project that shows how all of the pieces fit together.

You can use it as a starting point to create a custom web server for
handling specific requests or for improving your understanding of how
web servers work but don't try to use it for any production work
unless it is for a simple, low traffic site.

It is very easy to use, simply run it and it will start serving
content from the current directory.

   $ ./webserver.py

This will start up an HTTP server listening on port 8080 for the local
host using the current directory as the root of the web directory
handling requests with the built in default request handler.

You can then view the output using favorite browser:

   $ firefox localhost:8080

See the specific examples in the help below for more usage details.

One interesting feature of this system is that is allows embedded
python in HTML modules. This allows dynamic web pages to be created.

Here is a very simple example:

    <!DOCTYPE HTML>
    <!-- python
      params = locals()
      params['title'] = 'Title of Page'
      params['arg1'] = 'foo'
      params['arg2'] = 42
    -->
    <html>
      <head>
        <meta charset="utf-8">
        <title>{title}</title>
      </head>
      <body>
        <pre>
         arg1 = {arg1}
         arg2 = {arg2}
        </pre>
      </body>
    </html>

As you can see, the python code defines the values of variables that
used in the HTML.

The format of the variables is the same that used for string.format()
operations (string.Formatter objects) so they are very flexible.

The embedded python can also reference other files. Here is an
example of how that works:

    <!DOCTYPE html>
    <!-- python
      # Define the variables used on the page using
      # the full power of python.
      # The params dictionary is defined by the
      # request handler.
      import datetime
      import os
      import sys

      def read_file(params, ifn):
        """
        Read a file.
        """
        try:
          ifn = os.path.join(params['sysdir'], ifn)
          with open(ifn, 'r') as ifp:
            return ifp.read()
        except IOError as exc:
          return 'Read failed for {0}: {1!r}.'.format(ifn, exc)

      params = locals()

      params['page_header'] = read_file(params, 'page_header.html')
      params['page_footer'] = read_file(params, 'page_footer.html')
      params['title'] = 'Template Test of Embedded Python'

      # This is referenced by the page_header after the substitution.
      params['date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

      params['python_version'] = 'Python {0}.{1}.{2}'.format(sys.version_info[0],
                                                             sys.version_info[1],
                                                             sys.version_info[2])
      data = ''
      for i in range(5):
         data += '   {0} Count {0}\n'.format(i)
      params['data'] = data[:-1]  # strip the last new line

      params['top'] = '<a href="/">Top</a>'
    -->
    <html>
      <head>
        <meta charset="utf-8">
        <title>Template Test</title>
        <link rel="icon" type="image/png" href="/webserver.png">
        <link href="/webserver.css" rel="stylesheet">
        <script src="/webserver.js"></script>
      </head>
      <body>
        <!-- page header (references {title} and {date}) -->
        {page_header}

        <!-- page body -->
        <pre>
    {python_version}

    Loop Data
    {data}
        </pre>
    {top}
        <!-- page footer -->
        {page_footer}
      </body>
    </html>

The python code will be left justified automatically but other than
that it must have proper indenting.
'''
from __future__ import print_function

# Compatibility testing.
import sys
pver = 'Python {0}.{1}.{2}'.format(sys.version_info[0],
                                   sys.version_info[1],
                                   sys.version_info[2])
if sys.version_info[0] == 3:
    # There are some known problems with
    # byte/string conversions.
    import http.server as HTTPServer
    sys.exit('Sorry, {0} is not supported.'.format(pver))
elif sys.version_info[0] == 2:
    if sys.version_info[0] == 2 and sys.version_info[1] < 7:
        sys.exit('Sorry, {0} is not supported. You must use Python 2.7 or later.'.format(pver))
    import SimpleHTTPServer as HTTPServer
else:
    sys.exit('Sorry, {0} is not supported.'.format(pver))
del pver

# Standard imports.
import argparse
import cgi
import Cookie
import imp
import logging
import logging.handlers
import mimetypes
import random
import re
import os
import select
import socket
import SocketServer
import ssl
import string
import subprocess


VERSION = '1.0'


def logger_init(opts, name, level=logging.INFO):
    '''
    Create a basic logger with a stdout stream handler.
    '''
    logger = logging.getLogger(name)
    handler = logging.StreamHandler(stream=sys.stdout)
    formatter = logging.Formatter(opts.log_format)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(level)
    return logger


def logger_update(opts, logger):
    '''
{'fraction_non_alphanumeric': 0.10216388790351189, 'fraction_numerical': 0.00957786449095424, 'mean_word_length': 2.504350528278434, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2915353', 'n_tokens_mistral': 3245, 'n_tokens_neox': 3126, 'n_words': 1235}