Skip to content

Instantly share code, notes, and snippets.

@lmzach09
Created September 1, 2019 05:22
Show Gist options
  • Select an option

  • Save lmzach09/e4bb1dd4de2da700fd95658fd6d76acc to your computer and use it in GitHub Desktop.

Select an option

Save lmzach09/e4bb1dd4de2da700fd95658fd6d76acc to your computer and use it in GitHub Desktop.
HTTP server for a ChatBot API that queries Google for its ChatBot answers
import http.server
import socketserver
from google_search import chatbot_query
PORT = 8080
DIRECTORY = 'public'
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
def do_POST(self):
self.send_response(200)
content_length = int(self.headers['Content-Length'])
post_body = self.rfile.read(content_length)
self.end_headers()
print('user query', post_body)
google_search_chatbot_reply = chatbot_query(post_body)
self.wfile.write(str.encode(google_search_chatbot_reply))
with socketserver.TCPServer(('', PORT), Handler) as httpd:
print('serving at port', PORT)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment