User avatar
faramon
Posts: 123
Joined: Sat Jun 11, 2016 8:36 am
Location: Croatia

Calling webpage from python script

Fri Jun 17, 2016 11:07 am

Hi,

I try to call my php send email script from another web server and calling from browser the email is sent.
Calling from python script on Raspberry Pi Jessie, this is not possible, error occure constantly while executing this response = urllib2.urlopen("http://www.mysite.info/sendemail.php?te ... mperature)).
Why? Someone have an idea?
My python code:

#!/usr/bin/python

import urllib2
import time
import sys
import os
import subprocess

while True:
try:
temperature_int = float(subprocess.check_output(["/opt/vc/bin/vcgencmd","measure_temp"]).replace("temp=","").replace("'C",""))
print temperature_int
if temperature_int >= 48:
#sendemail(temperature_int) # first i try to call function but nothing happens
print "sending email..."
response = urllib2.urlopen("http://www.mysite.info/sendemail.php?te ... mperature))
response.geturl()

else:
time.sleep(10) # Delay for 10 sec

except Exception as e:
print "error"
time.sleep(10) # Delay for 10 sec

def sendemail( temperature ):
print "sending email..."
response = urllib2.urlopen("http://www.mysite.info/sendemail.php?te ... mperature))
response.geturl()
time.sleep(10) # Delay for 10 sec

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Calling webpage from python script

Fri Jun 17, 2016 12:18 pm

What error do you get?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
faramon
Posts: 123
Joined: Sat Jun 11, 2016 8:36 am
Location: Croatia

Re: Calling webpage from python script

Fri Jun 17, 2016 12:24 pm

My error when RAISE is:

Traceback (most recent call last):
File "/var/www/html/sendemail.py", line 19, in <module>
response = urllib2.urlopen("http://www.mysite.info/sendemail.php?te ... ature_int))
File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 437, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 475, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Calling webpage from python script

Fri Jun 17, 2016 1:04 pm

Well a 404 error code is page not found which means that the page can't be found.
If you open a web browser on your Raspberry Pi and type the link, does it work?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
faramon
Posts: 123
Joined: Sat Jun 11, 2016 8:36 am
Location: Croatia

Re: Calling webpage from python script

Fri Jun 17, 2016 2:12 pm

My apologies to all.
As I am PHP, JAVA programmer and python is my new language, so I had Python, PHP and Java scripts with same name for sending email. Thanks for the bug, helped me. I probably would have lost a lot of time until I realized that I uploaded the wrong script.

BTW, this solution works calling external script if someone need the same.

Greetings,
Faramon

hello world :-)
Posts: 121
Joined: Sat Nov 14, 2015 7:12 am
Location: England, in an insecure cloud, ie. The Interwebs
Contact: Website Yahoo Messenger AOL

Re: Calling webpage from python script

Mon Jun 20, 2016 2:15 pm

faramon wrote:...
BTW, this solution works calling external script if someone need the same.

Greetings,
Faramon
This has given me a few ideas. Bookmarking...
I do moral support. Here: https://www.raspberrypi.org/forums/search.php?search_id=egosearch
I know I use too many parentheses. Problem?
Topics I have posted in: http://bit.ly/1NbDdr5
My topics: http://bit.ly/1ObnKqQ
All my posts: http://bit.ly/1OHzje7

Return to “General discussion”