Skip to content

Instantly share code, notes, and snippets.

@kosiara
Created September 21, 2017 07:57
Show Gist options
  • Save kosiara/2b5d2633f70536f87f8cffa413207f90 to your computer and use it in GitHub Desktop.
Save kosiara/2b5d2633f70536f87f8cffa413207f90 to your computer and use it in GitHub Desktop.
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
Copy link
Author

kosiara commented Sep 21, 2017

If you don't have Python installed:

macOS:
brew install python3

ubuntu:
sudo apt install python3

@kosiara
Copy link
Author

kosiara 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