Simple Serve
#1
Quickly rewrote Vector's simple serve to have Python 3 compatibility, tab-completion, PEP8 formatting, and zero dependencies (besides python preinstalled).

Code:
#!/usr/bin/env python
import os
from getpass import getuser

# Python 2 Compatability
try:
   input = raw_input
except NameError:
   pass

try:
   from SimpleHTTPServer import SimpleHTTPRequestHandler
   import SocketServer
except ImportError:
   from http.server import SimpleHTTPRequestHandler as SimpleHTTPRequestHandler
   import socketserver as SocketServer

# Tab completion
try:
   import readline
   readline.parse_and_bind("tab: complete")
except ImportError:
   pass


USER_ABORT = "\n[!] User Aborted."
DEFAULT_PORT = 8000
DEFAULT_IP = "127.0.0.1"


def quickshell():
   cwd = os.getcwd()
   print("[+] OS Shell in " + cwd)
   print("[+] Enter 'Q' to quit")

   try:
       while True:
           command = input("\n%s$ " % getuser())
           if command.lower().strip() == 'q':
               print("\n[!] Exiting shell.")
               break
           os.system(command)
   except KeyboardInterrupt:
       print(USER_ABORT)


def main():
   print("\n[+] Basic HTTP Server.\n")

   default = input(
       "[?] Default Config? [Y]es/[N]o: ").lower()[0]

   if default == 'y':
       port = DEFAULT_PORT
       ip_address = DEFAULT_IP
       print("\n[+] Default settings loaded.\n")
   else:
       print("[+] Specify custom values.\n")
       port = int(input("Enter port %d: " % DEFAULT_PORT) or DEFAULT_PORT)
       ip_address = input("Enter host %s: " % DEFAULT_IP) or DEFAULT_IP

       print("\n[+] Invoke a shell to make adjustments in server directory?")
       invoke = input("[?] [Y]es/[N]o: ").lower()[0]
       if invoke == 'y':
           quickshell()
       else:
           print("[+] Okay.")

   print("[+] Starting Server.\n")

   handler = SimpleHTTPRequestHandler
   handler.extensions_map.update({
       '.webapp': 'application/x-web-app-manifest+json',
   })

   try:
       httpd = SocketServer.TCPServer((ip_address, port), handler)
   except Exception as e:
       print(
           "\n[!] Critical. An exception was raised with the following error message")
       print(str(e))

   # Catching keyboard interrupt for aesthetics purposes
   try:
       print("[+] Serving at %s:%d" % (ip_address, port))
       httpd.serve_forever()
   except KeyboardInterrupt:
       print(USER_ABORT)


if __name__ == "__main__":
   main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [TOOL] Blackpearl simple toolbox Whoami 2 22,588 10-09-2018, 06:21 PM
Last Post: Whoami
  [Python]Ever need to serve some files locally? Vector 7 29,684 02-16-2018, 04:45 PM
Last Post: Vector
  [Python] A simple music downloader Jake 0 10,315 08-13-2015, 02:50 PM
Last Post: Jake