title: How would I go about running a discord.py bot in the same program as an HTTP server?
body: <p>Goal: I'm trying to handle POST requests from Trello webhooks, then send an embed with relevant data on a discord guild.</p>
  
  <p>Current Progress: I got handling POST requests done, and I know how to send an embed and such. However, when it finally got to a point where I need to implement the two <em>together</em>, I realized it wouldn't be possible to just <code>py trelloHandler.py</code> and they both start running. I did some research and found <a href="https://stackoverflow.com/questions/48693069/running-flask-a-discord-bot-in-the-same-application">someone asking a similar question</a>. To most people this would be helpful, though I am pretty new to python (I like working on projects to learn) and not sure how I'd implement threading. I did manage to find a <a href="https://realpython.com/intro-to-python-threading/#what-is-a-thread" rel="nofollow noreferrer">guide over on realpython.com</a>, but I'm failing to understand it.</p>
  
  <p>My Question (TL;DR): How can I run an HTTP server that will listen to post requests in the same program as a discord.py bot (more specifically, using threading)?</p>
  
  <p>My Code ("bot_token_here" is replaced with my discord token):</p>
  
  <pre><code>import discord
  import json
  from http.server import HTTPServer, BaseHTTPRequestHandler
  from discord.ext import commands
  
  client = commands.Bot(command_prefix = "~")
  
  class requestHandler(BaseHTTPRequestHandler):
      def do_POST(self):
          # Interpret and process the data
          content_len = int(self.headers.get('content-length', 0))
          post_body = self.rfile.read(content_len)
          data = json.loads(post_body)
  
          # Action and Models data
          action = data['action']
          actionData = action['data']
          model = data['model']
  
          # Board and card data
          board = action['data']['board']
          card = action['data']['card']
  
          # Member data
          member = action['memberCreator']
          username = member['username']
  
          # Keep at end of do_POST
          self.send_response(204)
          self.send_header('content-type', 'text/html')
          self.end_headers()
  
      def do_HEAD(self):
          self.send_response(200)
          self.end_headers()
  
  @client.event
  async def on_ready():
      print("Bot is online, and ready to go! (Listening to {} servers!)".format(len(list(client.guilds))))
  
  def main():
      PORT = 9090
      server_address = ('localhost', PORT)
      server = HTTPServer(server_address, requestHandler)
      server.serve_forever()
      client.run("bot_token_here")
  
  if __name__ == '__main__':
      main()
  </code></pre>