Python Twisted
WebServer with Python Twisted
cat << 'EOF' > main.py
from twisted.web import server, resource
from twisted.internet import reactor, endpoints
class Counter(resource.Resource):
isLeaf = True
numberRequests = 0
def render_GET(self, request):
client_ip = request.getClientAddress().host
r=request.uri.decode('utf-8')
if not r =="/favicon.ico":
self.numberRequests += 1
request.setHeader(b"content-type", b"text/plain")
content = u"I am request #{} from {}\n".format(self.numberRequests, client_ip)
return content.encode("ascii")
endpoints.serverFromString(reactor, "tcp:8080").listen(server.Site(Counter()))
reactor.run()
EOF
Run
poetry init
poetry add twisted
poetry run python main.py
Browse
Open your Browser: http://ip-of-your-host:8080