Last active
May 27, 2023 21:51
-
-
Save ItsCubeTime/83e89d20e3a707b2833dbb6707ee9921 to your computer and use it in GitHub Desktop.
Basic Standard Library Python http servers that responds with generated html containing the requested url
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
Performance tests were performed on an i9-12900K running Microsoft Windows 11 [Version 10.0.22000.1817] & Python 3.10.2. |
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
# Used to measure the amount of requests a server can handle | |
import http.client | |
import ssl | |
import time | |
startTime = time.time() | |
lastPrint = startTime | |
numberOfIterations = 0 | |
while True: | |
# Set parameters for the request | |
host = "127.0.0.1" | |
port = 8000 | |
request_path = "/" | |
# Create an HTTPS connection object | |
conn = http.client.HTTPConnection(host, port, ) | |
conn.connect() | |
# Construct the request message | |
conn.request("GET", request_path) | |
# Retrieve the response | |
response = conn.getresponse() | |
# print(response.read().decode()) | |
numberOfIterations += 1 | |
currentTime = time.time() | |
if lastPrint + 2 < currentTime: | |
print(f""" | |
numerOfIterations: {numberOfIterations} | |
time: {time.time()-startTime}""") | |
lastPrint = currentTime |
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
# Handles 57816 requests in 10 seconds. | |
import socket | |
randomStr = """Based on your input, get a random alpha numeric string. The random string generator creates a series of numbers and letters that have no pattern. These can be helpful for creating security codes.With this utility you generate a 16 character output based on your input of numbers and upper and lower case letters. Random strings can be unique. Used in computing, a random string generator can also be called a random character string generator. This is an important tool if you want to generate a unique set of strings. The utility generates a sequence that lacks a pattern and is random.Throughout time, randomness was generated through mechanical devices such as dice, coin flips, and playing cards. A mechanical method of achieving randomness can be more time and resource consuming especially when a large number of randomized strings are needed as they could be in statistical applications. Computational random string generators replace the traditional mechanical devices. Possible applications for a random string generator could be for statistical sampling, simulations, and cryptography. For security reasons, a random string generator can be useful. The generation of this type of random string can be a common or typical task in computer programming. Some forms of randomness concern hash or seach algorithms. Another task that is random concerns selecting music tracks.In statistical theory, randomization is an important principle with one possible application involving survey sampling.Many applications of randomization have caused several methods to exist for generating random data. Lottery games is one current application. Slot machine odds are another use of random number generators.""" | |
HOST = '' | |
PORT = 8000 | |
def handle_request(client_socket): | |
request = client_socket.recv(1024).decode() | |
url = request.split()[1] | |
response = f"HTTP/1.1 200 OK\nContent-type: text/html\nContent-length: {len(randomStr)}\n\n<h1>Hello, world! You requested {url}</h1><p>{randomStr}</p>".encode() | |
client_socket.send(response) | |
client_socket.close() | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket: | |
server_socket.bind((HOST, PORT)) | |
server_socket.listen() | |
print(f"Serving on port {PORT}") | |
while True: | |
client_socket, address = server_socket.accept() | |
handle_request(client_socket) |
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
# Handles 25166 requests in 10 seconds. | |
import http.server | |
import socketserver | |
PORT = 8000 | |
class Handler(http.server.SimpleHTTPRequestHandler): | |
def do_GET(self): | |
url = self.path | |
response = f"<h1>Hello, world! You requested {url}<h1>".encode() | |
# set the response headers | |
self.send_response(200) | |
self.send_header('Content-type', 'text/html') # You can find a complete list of return types at https://www.iana.org/assignments/media-types/media-types.xhtml | |
# or a simplified list: https://stackoverflow.com/questions/23714383/what-are-all-the-possible-values-for-http-content-type-header | |
self.send_header('Content-length', len(response)) | |
self.end_headers() | |
# write the response | |
self.wfile.write(response) | |
with socketserver.TCPServer(("", PORT), Handler) as httpd: | |
print("serving at port", PORT) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment