import http.server as BaseHTTPServer
import socketserver as SocketServer
import urllib.parse as urlparse
import threading
import re
import argparse
import json
classapiHandler(BaseHTTPServer.BaseHTTPRequestHandler):defdo_GET(self):
self.server.safe_print("Currently %s threads are active."%(threading.activeCount()))
self.send_response(200)
self.send_header('Content-type','text/plain')
self.end_headers()# <scheme>://<netloc>/<path>?<query>#<fragment>(_, _, urlpath, urlparams, _)= urlparse.urlsplit(self.path)
response_dict ={}
response_dict['path']= urlpath
response_dict['params']= urlparams
response_str = json.dumps(
response_dict,
sort_keys=True,
indent=4,
separators=(',',': '))
self.wfile.write(response_str.encode("UTF-8"))classThreadedServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):def__init__(self,*args,**kwargs):
self.screen_lock = threading.Lock()
BaseHTTPServer.HTTPServer.__init__(self,*args,**kwargs)defsafe_print(self,*args,**kwargs):try:
self.screen_lock.acquire()print(*args,**kwargs)finally:
self.screen_lock.release()if __name__=="__main__":
parser=argparse.ArgumentParser()
parser.add_argument('-ip','--address',required=False,help='IP Address for the server to listen on. Default is 127.0.0.1',default='127.0.0.1')
parser.add_argument('port',type=int,help='You must provide a TCP Port to bind to')
args = parser.parse_args()#Setup the server.
server = ThreadedServer((args.address, args.port), apiHandler)#start the serverprint('Server is Ready. http://%s:%s/<command>/<string>'%(args.address, args.port))print('[?] - Remember: If you are going to call the api with wget, curl or something else from the bash prompt you need to escape the & with \& \n\n')whileTrue:try:
server.handle_request()except KeyboardInterrupt:break
server.safe_print("Control-C hit: Exiting server...")
server.safe_print("Web API Disabled...")
server.safe_print("Server has stopped.")