Skip to content

Instantly share code, notes, and snippets.

@m3dsec
Last active February 1, 2020 22:17
Show Gist options
  • Select an option

  • Save m3dsec/a335351d8d029a7357d4d5337bdb9355 to your computer and use it in GitHub Desktop.

Select an option

Save m3dsec/a335351d8d029a7357d4d5337bdb9355 to your computer and use it in GitHub Desktop.
#python3
import socket
# Define socket host and port
SERVER_HOST = '0.0.0.0'
SERVER_PORT = 8000
# Create socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((SERVER_HOST, SERVER_PORT))
server_socket.listen(1)
print('Listening on port %s ...' % SERVER_PORT)
while True:
# Wait for client connections
client_connection, client_address = server_socket.accept()
# Get the client request
request = client_connection.recv(1024).decode()
print(request)
# Parse HTTP headers
headers = request.split('\n')
filename = headers[0].split()[1]
# Get the content of the file
if filename == '/':
pass
filename = '/index.html'
try:
fin = open(filename)
content = fin.read()
fin.close()
response = 'HTTP/1.0 200 OK\n\n' + content
except FileNotFoundError:
response = 'HTTP/1.0 404 NOT FOUND\n\nFile Not Found'
# Send HTTP response
client_connection.sendall(response.encode())
client_connection.close()
# Close socket
server_socket.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment