0

Note: 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,


Setup:

My site folder looks like:

- templates 
    - ... front-end files only (html, css, js, etc)
- package.json
- Procfile
- requirements.txt
- runtime.txt
- server.py

Following are the files named above,

package.json

{
    "name": "site",
    "version": "1.0.0",
    "scripts": {
        "start": "python server.py"
    },
    "dependencies": {},
    "keywords": [],
    "author": "",
    "license": "ISC"
}

Procfile

web: python server.py $PORT

requirements.txt

gunicorn==20.0.4

runtime.txt

python-3.6.10

server.py

from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import re
from binascii import a2b_base64

rExt = re.compile(r'(?<=\.).+')
rTimeStamp = re.compile(r'(?<=\"timeStamp\":)\s*\d+')
rVote = re.compile(r'(?<=\"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()

Problem: After I followed all the commands given in the post (without any errors), I successfully deployed my HerokuApp, but on opening "https://rahulverma-blog.herokuapp.com/", I get the application Error. As, it suggests to run heroku logs --tail, I did and got the following,

... (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

Not Important, IMO...

Further, I tried googling about my problem and I found "Python Flask Web API [Heroku]: It runs locally but shows Application Error when deployed". Then, I tried editing my Procfile accordingly, but it didn't worked out, perhaps bcoz Sherman's server was made using Flask. BTW, I got OSError: Address already in use. The logs are following,

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 "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-04-29T08:14:12.493656+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-04-29T08:14:12.493656+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
2020-04-29T08:14:12.493657+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
2020-04-29T08:14:12.493657+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 678, in exec_module
2020-04-29T08:14:12.493658+00:00 app[web.1]: File "<frozen importlib._bootstrap>", 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 <module>
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)
vrintle
  • 5,501
  • 2
  • 16
  • 46
  • 2
    In your `server.py` you are binding to port 8080 instead of `os.environ['PORT']` – Tin Nguyen Apr 29 '20 at 10:42
  • @TinNguyen: thanks for responding, I'm editing server.py. Let me check if it works! Oops, I forgot to `str -> int` – vrintle Apr 29 '20 at 10:51
  • 1
    @TinNguyen: Bingooooo!!! The error vanished, and my site is working as smoothly as it did on localhost. Thanks man, you've became memorable :) Check it out, and leave a comment, if you feel lucky! – vrintle Apr 29 '20 at 11:48

0 Answers0