Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An attempt to create the simplest HTTP Hello world in Python3
import http.server
import socketserver
from http import HTTPStatus
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(HTTPStatus.OK)
self.end_headers()
self.wfile.write(b'Hello world')
httpd = socketserver.TCPServer(('', 8000), Handler)
httpd.serve_forever()
# python3 server.py
# 127.0.0.1 - - [11/Apr/2017 11:36:49] "GET / HTTP/1.1" 200 -
# http :8000
'''
HTTP/1.0 200 OK
Date: Tue, 11 Apr 2017 15:36:49 GMT
Server: SimpleHTTP/0.6 Python/3.5.2
Hello world
'''
@karlcow

This comment has been minimized.

Copy link

@karlcow karlcow commented Apr 12, 2017

Python 3.5 seulement.

@marga-google

This comment has been minimized.

Copy link

@marga-google marga-google commented Dec 18, 2019

What's the license for this gist?

@davidbgk

This comment has been minimized.

Copy link
Owner Author

@davidbgk davidbgk commented Dec 18, 2019

@margamanterola consider it to be in the public domain, out of curiosity is it for educational purpose?

@marga-google

This comment has been minimized.

Copy link

@marga-google marga-google commented Dec 18, 2019

Yes, for educational purposes.

@davidbgk

This comment has been minimized.

Copy link
Owner Author

@davidbgk davidbgk commented Dec 18, 2019

Glad it helps! 🙌

@vaibhavkumar049

This comment has been minimized.

Copy link

@vaibhavkumar049 vaibhavkumar049 commented Aug 4, 2020

hi can you add more examples like routes localhost:PORT/home type and how to handle them

@davidbgk

This comment has been minimized.

Copy link
Owner Author

@davidbgk davidbgk commented Aug 4, 2020

hi can you add more examples like routes localhost:PORT/home type and how to handle them

@vaibhavkumar049 the do_GET method will perform a listing directory, if you want to serve static content - even index.html files - through the server make sure that URLs match your directory structure (NOT recommended for anything near production 😱)

@vaibhavkumar049

This comment has been minimized.

Copy link

@vaibhavkumar049 vaibhavkumar049 commented Aug 5, 2020

hi can you add more examples like routes localhost:PORT/home type and how to handle them

@vaibhavkumar049 the do_GET method will perform a listing directory, if you want to serve static content - even index.html files - through the server make sure that URLs match your directory structure (NOT recommended for anything near production scream)

Hi I did that it like this
def do_GET(self): if self.path=='/': do something elif self.path=='/home': do something else: do something

even you can use enums

@davidbgk

This comment has been minimized.

Copy link
Owner Author

@davidbgk davidbgk commented Aug 5, 2020

even you can use enums

@vaibhavkumar049 sure, at that point, you may even want to use a dedicated lib for that, like autoroutes (shameless plug).

@vaibhavkumar049

This comment has been minimized.

Copy link

@vaibhavkumar049 vaibhavkumar049 commented Aug 5, 2020

even you can use enums

@vaibhavkumar049 sure, at that point, you may even want to use a dedicated lib for that, like autoroutes (shameless plug).

yeah, I had very small task so I was fine with self.path , but sure I will keep that in mind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment