SIGforum
any python programmers around here?

This topic can be found at:
https://sigforum.com/eve/forums/a/tpc/f/320601935/m/4170041134

October 01, 2017, 04:20 PM
r0gue
any python programmers around here?
I have a script I'm trying to mod. Instead of controlling the Raspberry Pi GPIO pins, I want it to send an HTTP request. I guess a "get"?

The old code: <i>(specifically after the IF part where it starts muckin with the GPIO).</i>

class device_handler(debounce_handler):
"""Publishes the on/off state requested,
and the IP address of the Echo making the request.
"""
#TRIGGERS = {str(sys.argv[1]): int(sys.argv[2])}
#TRIGGERS = {"office": 52000}
TRIGGERS = {"dogdog": 52000,"catcat":51000}

def act(self, client_address, state, name):
print("State", state, "from client @", client_address)
# GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
# GPIO.setup(int(7), GPIO.OUT) ## Setup GPIO Pin to OUTPUT
# GPIO.output(int(7), state) ## State is true/false
if name=="dogdog":
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(int(7), GPIO.OUT) ## Setup GPIO Pin to OUTPUT
GPIO.output(int(7), state) ## State is true/false
elif name =="catcat":
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(int(11), GPIO.OUT) ## Setup GPIO Pin to OUTPUT
GPIO.output(int(11), state) ## State is true/false

What I want is to replace that with code that will send an HTTP command out the ethernet port:
this --> http://192.168.1.4/30000/03

Then I'll want a little delay (1/2 second), then I'll send the off command. Then my evil genius thingy will do cool stuff and I'll post about it here! Smile




October 01, 2017, 10:54 PM
SirBeep
I only do a slight lil bit of python, so I don't know off the top of my head which version you're likely to be using, but a quick one liner in python 3:

import urllib.request
urllib.request.urlopen("http://192.168.1.4/30000/03").read()

slightly differnt in 2. or you may have higher level requests library available:
import requests
r = requests.get("http://192.168.1.4/30000/03")
October 01, 2017, 11:14 PM
Hawkins
quote:
import requests
r = requests.get("http://192.168.1.4/30000/03")


SirBeep's basically got it.

http requests are dead easy in python with requests or urllib

If you can understand what a GET or POST request *is*, then you can do it easily, no mucking around with network code.


--------------
July NoVA Sigshoot: Shooter's Paradise; 0900 (9AM) 23July05

My Signature is almost a decade out of date!
October 02, 2017, 07:02 PM
r0gue
Thanks guys! I'll give it a shot.




October 04, 2017, 08:33 PM
r0gue
Got it working, and got the delay to work. But the relay board calls for 12v and I only have a 5v supply, so while the LED does it's thing to indicate it's working, the relay won't throw. All my donor supplies are from USB, so 5v. I'll have to scrounge up a 12v source before I can show off my franken light switch.