title: Heroku: "Application Error" when using python http.server (not flask / django)
body: <p><strong>Note:</strong> I've started with Heroku and Git from today morning. So, please bear with me. I'm using Windows 10, Firefox, Python 3.6.10 - http.server. Also, I'm following a Medium post,</p>
  
  <ul>
  <li><a href="https://becominghuman.ai/steps-to-create-and-deploy-python-web-app-on-heroku-95b6c4f570b0" rel="nofollow noreferrer">"Steps to Create and Deploy Python web app on Heroku"</a></li>
  </ul>
  
  <hr>
  
  <h2><strong>Setup:</strong></h2>
  
  <p>My <code>site</code> folder looks like:</p>
  
  <pre><code>- templates 
      - ... front-end files only (html, css, js, etc)
  - package.json
  - Procfile
  - requirements.txt
  - runtime.txt
  - server.py
  </code></pre>
  
  <p>Following are the files named above,</p>
  
  <p><strong><code>package.json</code></strong></p>
  
  <pre><code>{
      "name": "site",
      "version": "1.0.0",
      "scripts": {
          "start": "python server.py"
      },
      "dependencies": {},
      "keywords": [],
      "author": "",
      "license": "ISC"
  }
  </code></pre>
  
  <p><strong><code>Procfile</code></strong></p>
  
  <pre><code>web: python server.py $PORT
  </code></pre>
  
  <p><strong><code>requirements.txt</code></strong></p>
  
  <pre><code>gunicorn==20.0.4
  </code></pre>
  
  <p><strong><code>runtime.txt</code></strong></p>
  
  <pre><code>python-3.6.10
  </code></pre>
  
  <hr>
  
  <p><strong><code>server.py</code></strong></p>
  
  <pre><code>from http.server import BaseHTTPRequestHandler, HTTPServer
  import json
  import re
  from binascii import a2b_base64
  
  rExt = re.compile(r'(?&lt;=\.).+')
  rTimeStamp = re.compile(r'(?&lt;=\"timeStamp\":)\s*\d+')
  rVote = re.compile(r'(?&lt;=\"voters\":)\s*.+(?=\}$)')
  mimeMap = {
    'html': 'text/html',
    'css': 'text/css',
    'js': 'text/javascript',
    'json': 'text/json',
    'txt': 'text/plain',
    'ico': 'image/x-icon',
    'jpg': 'image/jpg',
    'png': 'image/png'
  }
  
  class Server(BaseHTTPRequestHandler):
      def _send_headers(self, ext):
          self.send_response(200)
          self.send_header('Content-Type', mimeMap[ext])
          self.end_headers()
  
      def do_GET(self):
          self.path = '/index.html' if self.path == '/' else self.path
          ext = re.findall(rExt, self.path)[0]
          self._send_headers(ext)
          with open('.' + self.path, 'rb') as file:
              content = file.read()
          self.wfile.write(content)
  
      # do_POST declared here, very long code...
  
  def startServer():
      serverAdd = ('', 8080)
      server = HTTPServer(serverAdd, Server)
      server.serve_forever()
  
  startServer()
  </code></pre>
  
  <p><strong>Problem:</strong> After I followed all the commands given in the post (without any errors), I successfully deployed my HerokuApp, but on opening "<a href="https://rahulverma-blog.herokuapp.com/" rel="nofollow noreferrer">https://rahulverma-blog.herokuapp.com/</a>", I get the <code>application Error</code>. As, it suggests to run <code>heroku logs --tail</code>, I did and got the following,</p>
  
  <pre><code>... (i'm quoting only post build logs here)
  2020-04-29T10:06:34.000000+00:00 app[api]: Build succeeded
  2020-04-29T10:07:30.116043+00:00 heroku[web.1]: State changed from starting to crashed
  2020-04-29T10:07:30.120089+00:00 heroku[web.1]: State changed from crashed to starting
  2020-04-29T10:08:31.583051+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=rahulverma-blog.herokuapp.com request_id=29ea0435-0f71-440d-91e8-76196d731e0f fwd="106.219.65.58" dyno= connect= service= status=503 bytes= protocol=https
  2020-04-29T10:08:33.266420+00:00 heroku[web.1]: State changed from starting to crashed
  2020-04-29T10:08:35.232741+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=rahulverma-blog.herokuapp.com request_id=15f11f7f-35dd-49dc-b6f7-2461cfb9093a fwd="106.219.65.58" dyno= connect= service= status=503 bytes= protocol=https
  </code></pre>
  
  <hr>
  
  <p><strong>Not Important, IMO...</strong></p>
  
  <p>Further, I tried googling about my problem and I found <a href="https://stackoverflow.com/questions/43593542/python-flask-web-api-heroku-it-runs-locally-but-shows-application-error-when">"Python Flask Web API [Heroku]: It runs locally but shows Application Error when deployed"</a>. Then, I tried editing my <code>Procfile</code> accordingly, but it didn't worked out, perhaps bcoz Sherman's server was made using Flask. BTW, I got <code>OSError: Address already in use</code>. The logs are following,</p>
  
  <pre><code>2020-04-29T08:14:12.493652+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
  2020-04-29T08:14:12.493652+00:00 app[web.1]: self.callable = self.load()
  2020-04-29T08:14:12.493653+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
  2020-04-29T08:14:12.493653+00:00 app[web.1]: return self.load_wsgiapp()
  2020-04-29T08:14:12.493653+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
  2020-04-29T08:14:12.493653+00:00 app[web.1]: return util.import_app(self.app_uri)
  2020-04-29T08:14:12.493654+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
  2020-04-29T08:14:12.493654+00:00 app[web.1]: mod = importlib.import_module(module)
  2020-04-29T08:14:12.493654+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
  2020-04-29T08:14:12.493655+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
  2020-04-29T08:14:12.493655+00:00 app[web.1]: File "&lt;frozen importlib._bootstrap&gt;", line 994, in _gcd_import
  2020-04-29T08:14:12.493656+00:00 app[web.1]: File "&lt;frozen importlib._bootstrap&gt;", line 971, in _find_and_load
  2020-04-29T08:14:12.493656+00:00 app[web.1]: File "&lt;frozen importlib._bootstrap&gt;", line 955, in _find_and_load_unlocked
  2020-04-29T08:14:12.493657+00:00 app[web.1]: File "&lt;frozen importlib._bootstrap&gt;", line 665, in _load_unlocked
  2020-04-29T08:14:12.493657+00:00 app[web.1]: File "&lt;frozen importlib._bootstrap_external&gt;", line 678, in exec_module
  2020-04-29T08:14:12.493658+00:00 app[web.1]: File "&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed
  2020-04-29T08:14:12.493658+00:00 app[web.1]: File "/app/server.py", line 90, in &lt;module&gt;
  2020-04-29T08:14:12.493658+00:00 app[web.1]: startServer()
  2020-04-29T08:14:12.493659+00:00 app[web.1]: File "/app/server.py", line 87, in startServer
  2020-04-29T08:14:12.493659+00:00 app[web.1]: server = HTTPServer(serverAdd, Server)
  2020-04-29T08:14:12.493659+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/socketserver.py", line 456, in __init__
  2020-04-29T08:14:12.493660+00:00 app[web.1]: self.server_bind()
  2020-04-29T08:14:12.493660+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/http/server.py", line 136, in server_bind
  2020-04-29T08:14:12.493660+00:00 app[web.1]: socketserver.TCPServer.server_bind(self)
  2020-04-29T08:14:12.493661+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/socketserver.py", line 470, in server_bind
  2020-04-29T08:14:12.493661+00:00 app[web.1]: self.socket.bind(self.server_address)
  2020-04-29T08:14:12.493661+00:00 app[web.1]: OSError: [Errno 98] Address already in use
  2020-04-29T08:14:12.493733+00:00 app[web.1]: [2020-04-29 08:14:12 +0000] [11] [INFO] Worker exiting (pid: 11)
  </code></pre>