Created
September 21, 2017 07:57
-
-
Save kosiara/2b5d2633f70536f87f8cffa413207f90 to your computer and use it in GitHub Desktop.
Simple Python3 HTTP Server; one-line python GET method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python2 verion available here