Python Forum
appending variabl a URL for requests.post step
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
appending variabl a URL for requests.post step
#1
I use one of my RPis for monitoring my fridge temp. I currently have LEDs lit to indicate a good or bad condition. I also get a push notification when the monitoring service starts (cronjob @reboot). The code is a loop that checks the temps every 5 minutes and will send me a push notification (using requests.post and Android apps Join and Tasker). Using Tasker, I can have my phone respond to incoming messages (not just text messages - think pushes). I want the pi to attach a variable to the base of the URL so I can catch the event, where Tasker can alert me, showing the issue.

import requests


base = https://autoremotejoaomgcd.appspot.com/sendmessagekey=goo.gl:lonstrinofcharactersthatyoudonotneedforthis&message=

alert = overtempfreezer

r = requests.post("base + alert")
So the "base" variable will always be that. Never changes. It has been edited for privacy. At the end is "&message=" where the next characters would be the "alert" variable. I am hoping to combine the ougoing requests.post to be "https://autoremotejoaomgcd.appspot.com/sendmessagekey=goo.gl:lonstrinofcharactersthatyoudonotneedforthis&message=overtempfreezer"

Why not just combine them? There are other conditions that could be populated to be the "alert" variable, such as alert = overtempfridge, or alert = freezerdooropen.

How do I append the "alert" variable to the "base" and send it with a requests.post?
Reply
#2
So I've made progress with this, where it combines the two parts. It keeps failing on line 11.
import requsts

base = "autoremotejoaomgcd.appspot.com/sendmessage?key=goo.gl:editedforprivacy&message="

alert = "overtemp"

posturl = base + alert

print(posturl)

r = requests.post("https://(posturl)")
In shell, it produces "autoremotejoaomgcd.appspot.com/sendmessage?key=goo.gl:editedforprivacy&message=overtemp" so I assumed that I could just build line 11 as shown in my code. When I do that, I get this error, (note that my real world test does not have the "editedforprivacy" - it has the real key):

ConnectionError: HTTPSConnectionPool(host='(posturl)', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7585f750>: Failed to establish a new connection: [Errno -2] Name or service not known'))
Reply
#3
Note the error it says it's trying to connect to the host (posturl), which doesn't exist.

If you want to expand the variable posturl in another string, you don't use parenthesis. You could use an f-string and curly braces like:

r = requessts.post(f"https://{posturl}")
Reply
#4
It has occurred to me that this is a long roundabout way of dissecting and reassembling a URL. Since I am not actually passing a variable to Tasker (such as a numeric value of temperature), I am simply relaying a state change (greater or less than a temperature). I could be just as easily creating a URL that has the "alert" populated at the end of the "base" for each possible condition. Since Tasker/Join/AutoRemote triggers on specific (sometimes case specific) keywords, it would be best if I only had it trigger on a few keywords.

Thank you to those who took the time to respond to this. While it is possible for this to work, it is waaaaay too much work on the other end to capture and react to the spectrum of the possible values returned as the "alert" variable. I shall be content with just having the pi react to threshold breaches and reporting them to the Android ecosystem that is my interface.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't log in with requests post LilDog 2 6,047 Mar-11-2019, 08:16 AM
Last Post: yourv3nom
  No route to host error when using requests.post in python on raspberry pi mariummalik22 0 4,068 Jan-06-2018, 08:34 PM
Last Post: mariummalik22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020