使用Python http.server部署Heroku遇Application Error求助
Hey there, let's troubleshoot your Heroku deployment issues one by one—you've got this! Let's break down the errors you're seeing and fix them step by step.
First: Fix the H20 (Boot Timeout) and H10 (App Crash) Errors
These errors usually pop up because your app isn't starting correctly, isn't listening on the right port, or takes too long to boot. Since you're using a custom http.server handler instead of a framework like Flask, we need to align your setup with Heroku's requirements.
1. Correct Your Procfile
It sounds like you tweaked your Procfile but still hit snags—let's make sure it's set up properly. Your Procfile must be named exactly Procfile (no .txt extension) and contain this line:
web: python server.py
Double-check that server.py lives in your project's root folder (not nested in a subfolder) so Heroku can locate it easily.
2. Bind to Heroku's Dynamic Port (Critical Fix!)
Heroku doesn't allow hardcoded ports like 8000 or 5000—it assigns a unique, dynamic port via the PORT environment variable. If your server.py uses a fixed port, that's why you're getting crashes and later the "Address already in use" error.
Update your server.py to read this environment variable:
import os from http.server import HTTPServer, BaseHTTPRequestHandler class CustomHandler(BaseHTTPRequestHandler): # Your existing handler logic goes here... if __name__ == "__main__": # Use Heroku's PORT, or fall back to 5000 for local testing port = int(os.environ.get("PORT", 5000)) # Listen on all network interfaces (required for Heroku routing) server_address = ("0.0.0.0", port) httpd = HTTPServer(server_address, CustomHandler) print(f"Starting server on port {port}...") httpd.serve_forever()
The 0.0.0.0 is key here—it tells the server to accept traffic from all network interfaces, which Heroku needs to route requests to your app.
3. Ensure Your App Boots Quickly
H20 errors trigger if your app takes longer than 60 seconds to start. If your server.py has slow initialization steps (like loading large datasets), try moving those to after the server starts or optimizing them. For a basic http.server app, this shouldn't be an issue once the port is set correctly.
Fixing the "OSError: Address already in use" Error
This happens when your app tries to bind to a port that's already occupied by another process on Heroku's dyno. The fix is exactly the port variable step above—stop hardcoding ports and use Heroku's provided PORT variable. Heroku guarantees this port is unique for your dyno, so you won't hit conflicts once you switch to this setup.
Additional Checks to Avoid Future Issues
- Add a
requirements.txtfile: Even if you're only using standard library modules, Heroku expects this file. You can create an empty one, or list any external dependencies if you're using them. For Python 3.6.10, also add aruntime.txtfile with the linepython-3.6.10(Heroku supports specific versions—confirm this one is still supported if you hit version-related errors). - Test locally first: Run
python server.pyon your Windows machine, and verify it starts without errors and you can access it athttp://localhost:5000. This catches issues before you deploy to Heroku. - Check logs in real-time: Use the command
heroku logs --tailin your terminal to see exactly what's happening when your app tries to start. This will show you tracebacks or hidden errors that aren't visible on the web error page.
Once you make these changes, redeploy your app with git add ., git commit -m "Fix port binding and Procfile", and git push heroku main (or your Heroku branch name). That should resolve the H20, H10, and port errors.
内容的提问来源于stack exchange,提问作者vrintle

