Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Python3 HTTP Server; one-line python GET method
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse
import subprocess
class GetHandler(BaseHTTPRequestHandler):
ls_l_cmd = ['ls','-l']
def do_GET(self):
parsed_path = urlparse(self.path)
cons_res = subprocess.run(GetHandler.ls_l_cmd, stdout=subprocess.PIPE)
message = '\n'.join([
'Simple Python3 HTTP server',
'ls -l command result: \n %s' % cons_res.stdout,
'',
]).replace('\\n', '\n')
self.send_response(200)
self.end_headers()
message_bytes = str.encode(message)
self.wfile.write(message_bytes)
return
if __name__ == '__main__':
from http.server import HTTPServer
server = HTTPServer(('localhost', 8080), GetHandler)
print ('Starting server at http://localhost:8080')
server.serve_forever()
@kosiara

This comment has been minimized.

Copy link
Owner Author

commented Sep 21, 2017

If you don't have Python installed:

macOS:
brew install python3

ubuntu:
sudo apt install python3

@kosiara

This comment has been minimized.

Copy link
Owner Author

commented Sep 21, 2017

Python2 verion available here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.