import socket import os.path import subprocess extention_list = [".py", ".bat", ".sh"] print("Starting server...") srv = socket.socket() error_path = "error.html" host = "127.0.0.1" port = 80 srv.bind((host, port)) srv.listen(10) print("Listening for connections...") while True: c, addr = srv.accept() print("") print("Got connection from: " + addr[0]) data = c.recv(1024) string = data.decode("utf-8") print("Size of data recieved: " + str(len(string))) if "GET " in string: dataOveride = False print("Initiating file transfer...") s = string.index("GET ") v = string.index("HTTP/") f = string.index("\r\n") request = string[int(s):int(f)] print("HTTP version: " + string[int(v)+5:int(f)]) print("Request query: " + request) website_path = request[5:-9] print("Raw website path: " + website_path) for i in extention_list: print("Checking for " + i) if i in website_path: print("Found " + i) if website_path.rindex(i)+len(i) == len(website_path): start = website_path.rfind('/') if start == -1: start = 0 if os.path.isfile(website_path[start:]): print("Running script...") ret = subprocess.check_output(website_path[start:], stderr=subprocess.STDOUT, shell=True) website_data = ret.decode("utf-8") dataOveride = True else: print("Script was not found...") dataOveride = True website_file = open(error_path, 'rb') website_data = website_file.read() website_file.close() website_data = website_data.decode("utf-8") if dataOveride == False: if '.' not in website_path: if website_path == "": website_path += "index.html" else: website_path += "/index.html" print("Did not find the path, adding /index.html to see if it helps") print("File path: " + website_path) if os.path.isfile(website_path): website_file = open(website_path, 'rb') print("File found") else: website_file = open(error_path, 'rb') print("File not found, using error file") website_data = website_file.read() website_file.close() else: website_data = website_data.encode("utf-8") print("Size of data to send: " + str(len(website_data))) print("Transfering file") if '.html' in website_path: tmp = website_data website_data = b"HTTP/1.1 200 OK\n" website_data += b"Content-type: text/html\n\n" website_data += tmp c.send(website_data) print("Closing connection with: " + addr[0]) c.close()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question