github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/containers/compilers/rump/python3/python-wrapper/python-wrapper-udp.py (about)

     1  from io import StringIO
     2  import sys
     3  import threading
     4  from http.server import BaseHTTPRequestHandler, HTTPServer
     5  
     6  ###From https://github.com/bmc/grizzled-python/blob/bf9998bd0f6497d1e368610f439f9085d019bf76/grizzled/io/__init__.py
     7  # ---------------------------------------------------------------------------
     8  # Imports
     9  # ---------------------------------------------------------------------------
    10  
    11  import os
    12  import zipfile
    13  
    14  class MultiWriter(object):
    15      """
    16      Wraps multiple file-like objects so that they all may be written at once.
    17      For example, the following code arranges to have anything written to
    18      ``sys.stdout`` go to ``sys.stdout`` and to a temporary file:
    19      .. python::
    20          import sys
    21          from grizzled.io import MultiWriter
    22          sys.stdout = MultiWriter(sys.__stdout__, open('/tmp/log', 'w'))
    23      """
    24      def __init__(self, *args):
    25          """
    26          Create a new ``MultiWriter`` object to wrap one or more file-like
    27          objects.
    28          :Parameters:
    29              args : iterable
    30                  One or more file-like objects to wrap
    31          """
    32          self.__files = args
    33  
    34      def write(self, buf):
    35          """
    36          Write the specified buffer to the wrapped files.
    37          :Parameters:
    38              buf : str or bytes
    39                  buffer to write
    40          """
    41          for f in self.__files:
    42              f.write(buf)
    43  
    44      def flush(self):
    45          """
    46          Force a flush.
    47          """
    48          for f in self.__files:
    49              f.flush()
    50  
    51      def close(self):
    52          """
    53          Close all contained files.
    54          """
    55          for f in self.__files:
    56              f.close()
    57  
    58  logsbuf = StringIO()
    59  
    60  class Capturing(list):
    61      def __enter__(self):
    62          self._stdout = sys.stdout
    63          self._stderr = sys.stderr
    64          sys.stdout = self._stringioout = MultiWriter(logsbuf, self._stdout)
    65          sys.stderr = self._stringioerr = MultiWriter(logsbuf, self._stderr)
    66          return self
    67      def __exit__(self, *args):
    68          self.extend(logsbuf.getvalue().splitlines())
    69          self.extend(logsbuf.getvalue().splitlines())
    70          sys.stdout = self._stdout
    71          sys.stderr = self._stderr
    72  
    73  CONST_PORT=9967
    74  
    75  # HTTPRequestHandler class
    76  class LogServer(BaseHTTPRequestHandler):
    77    # GET
    78    def do_GET(self):
    79          # Send response status code
    80          self.send_response(200)
    81  
    82          # Send headers
    83          self.send_header('Content-type','text/html')
    84          self.end_headers()
    85  
    86          # Send message back to client
    87          message = logsbuf.getvalue()
    88          # Write content as utf-8 data
    89          self.wfile.write(bytes(message, "utf8"))
    90          return
    91  
    92  def run():
    93    print('starting server on port', CONST_PORT)
    94    server_address = ('0.0.0.0', CONST_PORT)
    95    httpd = HTTPServer(server_address, LogServer)
    96    print('running server...')
    97    httpd.serve_forever()
    98  
    99  with Capturing():
   100      os.chdir("/bootpart")
   101      t = threading.Thread(target = run)
   102      t.daemon = True
   103      t.start()
   104  
   105      import main.py ##NAME OF SCRIPT GOES HERE