Pushover脚本无报错但收不到推送,智能门铃项目求助
Hey there! Let's dig into why your Python 3 script isn't sending Pushover notifications even though it runs without throwing errors. I’ve looked over your code and have a few actionable fixes to try out:
1. Fix the Extra Spaces in Your API Keys
First off, I notice your app_key and user_key have extra spaces around them (like " app key "). Pushover’s API keys are strict—any leading/trailing whitespace will cause authentication to fail. Remove those spaces so your keys look like this:
app_key = "your_real_app_key" user_key = "your_real_user_key"
Double-check that you’re copying the exact keys from your Pushover dashboard (no typos or missing characters!).
2. Log the API Response to See Hidden Errors
Right now, your code calls conn.getresponse() but doesn’t actually read or print the response. Pushover might be sending back an error message (like invalid credentials or missing parameters) that you’re not seeing. Update your function to log the response details:
def pushOver(title,message,url): app_key = "your_real_app_key" user_key = "your_real_user_key" conn = http.client.HTTPSConnection("api.pushover.net:443") conn.request("POST", "/1/message.json", urllib.parse.urlencode({ "token": app_key, "user": user_key, "title": title, "message": message, "url": url, }), { "content-type": "application/x-www-form-urlencoded" } ) # Read and print the response to debug response = conn.getresponse() print(f"Status Code: {response.status}") print(f"Response Message: {response.reason}") print(f"Full Response: {response.read().decode('utf-8')}")
Run the script again—this will show you exactly what Pushover is sending back, which is critical for debugging.
3. Check the Empty url Parameter
You’re passing an empty string for url in your test call. While Pushover allows optional parameters, sometimes empty values can cause unexpected behavior. Try either:
- Omitting the
urlkey entirely from the payload if you don’t need it, or - Passing a valid test URL like
"https://example.com"to see if that resolves the issue.
4. Simplify with the requests Library (Optional but Recommended)
The http.client and urllib modules work, but they’re a bit clunky for API calls. The requests library makes this way easier and more readable. Here’s how to rewrite your function with it:
- First install the library:
pip install requests - Update your code:
import requests def pushOver(title, message, url): app_key = "your_real_app_key" user_key = "your_real_user_key" payload = { "token": app_key, "user": user_key, "title": title, "message": message } # Only add url if it's not empty if url.strip(): payload["url"] = url response = requests.post("https://api.pushover.net/1/message.json", data=payload) print("API Response:", response.json())
This will automatically handle encoding and give you a JSON response that’s easy to parse for errors.
5. Verify Pushover Account/Device Settings
Before diving deeper into code, double-check these basics:
- Make sure your user key matches the one in your Pushover Account Settings
- Confirm your app key is linked to the correct application you created in Pushover
- Ensure your target device is registered in your Pushover account and isn’t in Do Not Disturb mode
Start with the first fix (removing spaces from keys)—that’s the most common culprit here. Then use the response logging to see if any other issues pop up.
内容的提问来源于stack exchange,提问作者Ash Stirland

