Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35381 > unrolled thread
| Started by | Alexander Ranstam <ranstam@gmail.com> |
|---|---|
| First post | 2012-12-22 12:36 -0800 |
| Last post | 2012-12-23 19:31 -0500 |
| Articles | 14 — 9 participants |
Back to article view | Back to comp.lang.python
Python, email temperature Alexander Ranstam <ranstam@gmail.com> - 2012-12-22 12:36 -0800
Re: Python, email temperature KarlE <ranstam@gmail.com> - 2012-12-22 12:38 -0800
Re: Python, email temperature Joel Goldstick <joel.goldstick@gmail.com> - 2012-12-22 15:44 -0500
Re: Python, email temperature KarlE <ranstam@gmail.com> - 2012-12-22 12:54 -0800
Re: Python, email temperature Gary Herron <gary.herron@islandtraining.com> - 2012-12-22 13:12 -0800
Re: Python, email temperature KarlE <ranstam@gmail.com> - 2012-12-22 12:54 -0800
Re: Python, email temperature No One <atsidi@gmail.com> - 2012-12-22 21:53 +0000
Re: Python, email temperature Gary Herron <gary.herron@islandtraining.com> - 2012-12-22 12:45 -0800
Re: Python, email temperature KarlE <ranstam@gmail.com> - 2012-12-22 14:50 -0800
Re: Python, email temperature Chris Angelico <rosuav@gmail.com> - 2012-12-23 10:15 +1100
Re: Python, email temperature KarlE <ranstam@gmail.com> - 2012-12-23 05:46 -0800
Re: Python, email temperature Mitya Sirenef <msirenef@lightbird.net> - 2012-12-23 12:23 -0500
Re: Python, email temperature Dave Angel <d@davea.name> - 2012-12-23 15:34 -0500
Re: Python, email temperature Terry Reedy <tjreedy@udel.edu> - 2012-12-23 19:31 -0500
| From | Alexander Ranstam <ranstam@gmail.com> |
|---|---|
| Date | 2012-12-22 12:36 -0800 |
| Subject | Python, email temperature |
| Message-ID | <543ec063-14c1-4ca6-a911-ae6f58c22a8c@googlegroups.com> |
Hi!
Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
The two programs work very well on their own, but this doesnt work.
this works: server.sendmail(fromaddr, toaddrs, msg)
but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
despite the command "print cputemp" working in the same program.
When i run the program i get the error:
Traceback (most recent call last):
File "sendcpu.py", line 36, in <module>
msg = cpu_temperature
NameError: name 'cpu_temperature' is not defined
Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
Thanx!
//Alexander
[toc] | [next] | [standalone]
| From | KarlE <ranstam@gmail.com> |
|---|---|
| Date | 2012-12-22 12:38 -0800 |
| Message-ID | <1388673a-2baf-4ebc-8837-e92b88bfc8da@googlegroups.com> |
| In reply to | #35381 |
On Saturday, December 22, 2012 9:36:41 PM UTC+1, Alexander Ranstam wrote: > Hi! > > > > Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email. > > > > The two programs work very well on their own, but this doesnt work. > > > > this works: server.sendmail(fromaddr, toaddrs, msg) > > but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature) > > > > despite the command "print cputemp" working in the same program. > > > > When i run the program i get the error: > > > > Traceback (most recent call last): > > File "sendcpu.py", line 36, in <module> > > msg = cpu_temperature > > NameError: name 'cpu_temperature' is not defined > > > > Does anyone know why the program claims that cpu_temperature isnt defined, when it is? > > > > Thanx! > > > > //Alexander Typo: "print cputemp" should say "print cpu_temperature"
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2012-12-22 15:44 -0500 |
| Message-ID | <mailman.1208.1356209086.29569.python-list@python.org> |
| In reply to | #35381 |
[Multipart message — attachments visible in raw view] — view raw
On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam <ranstam@gmail.com>wrote: > Hi! > > Im totally new to Python, and im using it on my Raspberry pi. I found a > program that sends an email, and one that checks the temperature of my CPU, > but i cant seem to combine the to into the funktion that i want, sending me > the CPU temp via Email. > > The two programs work very well on their own, but this doesnt work. > > this works: server.sendmail(fromaddr, toaddrs, msg) > but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature) > > despite the command "print cputemp" working in the same program. > > When i run the program i get the error: > > Traceback (most recent call last): > File "sendcpu.py", line 36, in <module> > msg = cpu_temperature > NameError: name 'cpu_temperature' is not defined > > Does anyone know why the program claims that cpu_temperature isnt defined, > when it is? > You should copy and paste the code here including the context around the error. You say print cputemp works, but cpu_temperature is not defined. They are spelled differently. Start there > > Thanx! > > //Alexander > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick
[toc] | [prev] | [next] | [standalone]
| From | KarlE <ranstam@gmail.com> |
|---|---|
| Date | 2012-12-22 12:54 -0800 |
| Message-ID | <4d1cf59e-7fb0-4b93-bfe8-db484edb5a45@googlegroups.com> |
| In reply to | #35384 |
On Saturday, December 22, 2012 9:44:39 PM UTC+1, Joel Goldstick wrote:
> On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam <ran...@gmail.com> wrote:
>
> Hi!
>
>
>
> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
>
>
>
>
> The two programs work very well on their own, but this doesnt work.
>
>
>
> this works: server.sendmail(fromaddr, toaddrs, msg)
>
> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
>
>
>
> despite the command "print cputemp" working in the same program.
>
>
>
> When i run the program i get the error:
>
>
>
> Traceback (most recent call last):
>
> File "sendcpu.py", line 36, in <module>
>
> msg = cpu_temperature
>
> NameError: name 'cpu_temperature' is not defined
>
>
>
> Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
>
>
>
> You should copy and paste the code here including the context around the error. You say print cputemp works, but cpu_temperature is not defined. They are spelled differently. Start there
>
>
>
>
> Thanx!
>
>
>
> //Alexander
>
>
>
> --
>
> http://mail.python.org/mailman/listinfo/python-list
>
>
>
>
> --
> Joel Goldstick
Hi!
I made a typing error, and couldnt edit the post :( this is the code:
#!/usr/bin/env python
from __future__ import division
from subprocess import PIPE, Popen
import psutil
import smtplib
def get_cpu_temperature():
process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
output, _error = process.communicate()
return float(output[output.index('=') + 1:output.rindex("'")])
def main():
cpu_temperature = get_cpu_temperature()
cpu_usage = psutil.cpu_percent()
ram = psutil.phymem_usage()
ram_total = ram.total / 2**20 # MiB.
ram_used = ram.used / 2**20
ram_free = ram.free / 2**20
ram_percent_used = ram.percent
disk = psutil.disk_usage('/')
disk_total = disk.total / 2**30 # GiB.
disk_used = disk.used / 2**30
disk_free = disk.free / 2**30
disk_percent_used = disk.percent
#
# Print top five processes in terms of virtual memory usage.
#
print 'CPU temperature is: ', cpu_temperature
fromaddr = 'myemailadress'
toaddrs = 'myemailadress'
#msg = 'There was a terrible error that occured and I wanted you to know!'
msg = cpu_temperature
# Credentials (if needed)
username = 'myusername'
password = 'mypassword'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, cpu_temperature)
server.quit()
if __name__ == '__main__':
main()
running it gives the following error:
pi@raspberrypi /home/python $ python sendcpu.py
Traceback (most recent call last):
File "sendcpu.py", line 36, in <module>
msg = cpu_temperature
NameError: name 'cpu_temperature' is not defined
pi@raspberrypi /home/python $
isnt cpu_temperature defined?
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gary.herron@islandtraining.com> |
|---|---|
| Date | 2012-12-22 13:12 -0800 |
| Message-ID | <mailman.1214.1356211303.29569.python-list@python.org> |
| In reply to | #35387 |
On 12/22/2012 12:54 PM, KarlE wrote:
> On Saturday, December 22, 2012 9:44:39 PM UTC+1, Joel Goldstick wrote:
>> On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam <ran...@gmail.com> wrote:
>>
>> Hi!
>>
>>
>>
>> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
>>
>>
>>
>>
>> The two programs work very well on their own, but this doesnt work.
>>
>>
>>
>> this works: server.sendmail(fromaddr, toaddrs, msg)
>>
>> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
>>
>>
>>
>> despite the command "print cputemp" working in the same program.
>>
>>
>>
>> When i run the program i get the error:
>>
>>
>>
>> Traceback (most recent call last):
>>
>> File "sendcpu.py", line 36, in <module>
>>
>> msg = cpu_temperature
>>
>> NameError: name 'cpu_temperature' is not defined
>>
>>
>>
>> Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
>>
>>
>>
>> You should copy and paste the code here including the context around the error. You say print cputemp works, but cpu_temperature is not defined. They are spelled differently. Start there
>>
>>
>>
>>
>> Thanx!
>>
>>
>>
>> //Alexander
>>
>>
>>
>> --
>>
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>>
>>
>> --
>> Joel Goldstick
> Hi!
>
> I made a typing error, and couldnt edit the post :( this is the code:
>
>
> #!/usr/bin/env python
> from __future__ import division
> from subprocess import PIPE, Popen
> import psutil
> import smtplib
>
> def get_cpu_temperature():
> process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
> output, _error = process.communicate()
> return float(output[output.index('=') + 1:output.rindex("'")])
>
>
> def main():
> cpu_temperature = get_cpu_temperature()
> cpu_usage = psutil.cpu_percent()
>
> ram = psutil.phymem_usage()
> ram_total = ram.total / 2**20 # MiB.
> ram_used = ram.used / 2**20
> ram_free = ram.free / 2**20
> ram_percent_used = ram.percent
>
> disk = psutil.disk_usage('/')
> disk_total = disk.total / 2**30 # GiB.
> disk_used = disk.used / 2**30
> disk_free = disk.free / 2**30
> disk_percent_used = disk.percent
> #
> # Print top five processes in terms of virtual memory usage.
> #
> print 'CPU temperature is: ', cpu_temperature
>
> fromaddr = 'myemailadress'
> toaddrs = 'myemailadress'
> #msg = 'There was a terrible error that occured and I wanted you to know!'
> msg = cpu_temperature
>
> # Credentials (if needed)
> username = 'myusername'
> password = 'mypassword'
>
> # The actual mail send
> server = smtplib.SMTP('smtp.gmail.com:587')
> server.starttls()
> server.login(username,password)
> server.sendmail(fromaddr, toaddrs, cpu_temperature)
> server.quit()
>
>
>
>
> if __name__ == '__main__':
> main()
>
> running it gives the following error:
>
> pi@raspberrypi /home/python $ python sendcpu.py
> Traceback (most recent call last):
> File "sendcpu.py", line 36, in <module>
> msg = cpu_temperature
> NameError: name 'cpu_temperature' is not defined
> pi@raspberrypi /home/python $
>
>
> isnt cpu_temperature defined?
>
>
First: Learn about Python SCOPES. You are defining variables inside
(as local variables) the procedure main, but they are lost as soon as
main returns. If you want values computed inside main but available
outside main, you should return them.
Second: Some confusion over what order things are executed in. The
code in main is run when you call main -- ans that's at the very end of
the file. The lines before the call to main expect to use the value
cpu_temperature when you have not yet called main to compute the value
(and which doesn't even return the value as noted above).
The confusion is partly caused by having some of your code inside main
and some of it outside main and expecting the two parts to
communicate. I'd suggest putting everything up through the
server.quit() into procedure main.
[toc] | [prev] | [next] | [standalone]
| From | KarlE <ranstam@gmail.com> |
|---|---|
| Date | 2012-12-22 12:54 -0800 |
| Message-ID | <mailman.1211.1356209693.29569.python-list@python.org> |
| In reply to | #35384 |
On Saturday, December 22, 2012 9:44:39 PM UTC+1, Joel Goldstick wrote:
> On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam <ran...@gmail.com> wrote:
>
> Hi!
>
>
>
> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
>
>
>
>
> The two programs work very well on their own, but this doesnt work.
>
>
>
> this works: server.sendmail(fromaddr, toaddrs, msg)
>
> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
>
>
>
> despite the command "print cputemp" working in the same program.
>
>
>
> When i run the program i get the error:
>
>
>
> Traceback (most recent call last):
>
> File "sendcpu.py", line 36, in <module>
>
> msg = cpu_temperature
>
> NameError: name 'cpu_temperature' is not defined
>
>
>
> Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
>
>
>
> You should copy and paste the code here including the context around the error. You say print cputemp works, but cpu_temperature is not defined. They are spelled differently. Start there
>
>
>
>
> Thanx!
>
>
>
> //Alexander
>
>
>
> --
>
> http://mail.python.org/mailman/listinfo/python-list
>
>
>
>
> --
> Joel Goldstick
Hi!
I made a typing error, and couldnt edit the post :( this is the code:
#!/usr/bin/env python
from __future__ import division
from subprocess import PIPE, Popen
import psutil
import smtplib
def get_cpu_temperature():
process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
output, _error = process.communicate()
return float(output[output.index('=') + 1:output.rindex("'")])
def main():
cpu_temperature = get_cpu_temperature()
cpu_usage = psutil.cpu_percent()
ram = psutil.phymem_usage()
ram_total = ram.total / 2**20 # MiB.
ram_used = ram.used / 2**20
ram_free = ram.free / 2**20
ram_percent_used = ram.percent
disk = psutil.disk_usage('/')
disk_total = disk.total / 2**30 # GiB.
disk_used = disk.used / 2**30
disk_free = disk.free / 2**30
disk_percent_used = disk.percent
#
# Print top five processes in terms of virtual memory usage.
#
print 'CPU temperature is: ', cpu_temperature
fromaddr = 'myemailadress'
toaddrs = 'myemailadress'
#msg = 'There was a terrible error that occured and I wanted you to know!'
msg = cpu_temperature
# Credentials (if needed)
username = 'myusername'
password = 'mypassword'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, cpu_temperature)
server.quit()
if __name__ == '__main__':
main()
running it gives the following error:
pi@raspberrypi /home/python $ python sendcpu.py
Traceback (most recent call last):
File "sendcpu.py", line 36, in <module>
msg = cpu_temperature
NameError: name 'cpu_temperature' is not defined
pi@raspberrypi /home/python $
isnt cpu_temperature defined?
[toc] | [prev] | [next] | [standalone]
| From | No One <atsidi@gmail.com> |
|---|---|
| Date | 2012-12-22 21:53 +0000 |
| Message-ID | <kb5a5a$tal$1@dont-email.me> |
| In reply to | #35388 |
On 2012-12-22, KarlE <ranstam@gmail.com> wrote:
> On Saturday, December 22, 2012 9:44:39 PM UTC+1, Joel Goldstick wrote:
>> On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam <ran...@gmail.com> wrote:
>>
>> Hi!
>>
>>
>>
>> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
>>
>>
>>
>>
>> The two programs work very well on their own, but this doesnt work.
>>
>>
>>
>> this works: server.sendmail(fromaddr, toaddrs, msg)
>>
>> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
>>
>>
>>
>> despite the command "print cputemp" working in the same program.
>>
>>
>>
>> When i run the program i get the error:
>>
>>
>>
>> Traceback (most recent call last):
>>
>> ? File "sendcpu.py", line 36, in <module>
>>
>> ? ? msg = cpu_temperature
>>
>> NameError: name 'cpu_temperature' is not defined
>>
>>
>>
>> Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
>>
>>
>>
>> You should copy and paste the code here including the context around the error.? You say print cputemp works, but cpu_temperature is not defined.? They are spelled differently.? Start there
>>
>>
>>
>>
>> Thanx!
>>
>>
>>
>> //Alexander
>>
>>
>>
>> --
>>
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>>
>>
>> --
>> Joel Goldstick
>
> Hi!
>
> I made a typing error, and couldnt edit the post :( this is the code:
>
>
> #!/usr/bin/env python
> from __future__ import division
> from subprocess import PIPE, Popen
> import psutil
> import smtplib
>
> def get_cpu_temperature():
> process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
> output, _error = process.communicate()
> return float(output[output.index('=') + 1:output.rindex("'")])
>
>
> def main():
> cpu_temperature = get_cpu_temperature()
> cpu_usage = psutil.cpu_percent()
>
> ram = psutil.phymem_usage()
> ram_total = ram.total / 2**20 # MiB.
> ram_used = ram.used / 2**20
> ram_free = ram.free / 2**20
> ram_percent_used = ram.percent
>
> disk = psutil.disk_usage('/')
> disk_total = disk.total / 2**30 # GiB.
> disk_used = disk.used / 2**30
> disk_free = disk.free / 2**30
> disk_percent_used = disk.percent
> #
> # Print top five processes in terms of virtual memory usage.
> #
> print 'CPU temperature is: ', cpu_temperature
>
> fromaddr = 'myemailadress'
> toaddrs = 'myemailadress'
> #msg = 'There was a terrible error that occured and I wanted you to know!'
> msg = cpu_temperature
>
> # Credentials (if needed)
> username = 'myusername'
> password = 'mypassword'
>
> # The actual mail send
> server = smtplib.SMTP('smtp.gmail.com:587')
> server.starttls()
> server.login(username,password)
> server.sendmail(fromaddr, toaddrs, cpu_temperature)
> server.quit()
>
>
>
>
> if __name__ == '__main__':
> main()
>
> running it gives the following error:
>
> pi@raspberrypi /home/python $ python sendcpu.py
> Traceback (most recent call last):
> File "sendcpu.py", line 36, in <module>
> msg = cpu_temperature
> NameError: name 'cpu_temperature' is not defined
> pi@raspberrypi /home/python $
>
>
> isnt cpu_temperature defined?
>
>
You might have a look at the indentation, as well. At least on my reader, the lines from "fromaddr =" to "server.quit()" are in a left indented block from the main function. You probably want to get them all in the same indentation level to solve the scope issue. Also, on the code as it copies to my editor there are mixed tabs and spaces. If your code also mixes tabs and spaces, pick one (spaces preferred over tabs) and make sure to eliminate the other.
Norm.
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gary.herron@islandtraining.com> |
|---|---|
| Date | 2012-12-22 12:45 -0800 |
| Message-ID | <mailman.1209.1356209504.29569.python-list@python.org> |
| In reply to | #35381 |
On 12/22/2012 12:36 PM, Alexander Ranstam wrote: > Hi! > > Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email. > > The two programs work very well on their own, but this doesnt work. > > this works: server.sendmail(fromaddr, toaddrs, msg) > but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature) > > despite the command "print cputemp" working in the same program. > > When i run the program i get the error: > > Traceback (most recent call last): > File "sendcpu.py", line 36, in <module> > msg = cpu_temperature > NameError: name 'cpu_temperature' is not defined > > Does anyone know why the program claims that cpu_temperature isnt defined, when it is? > > Thanx! > > //Alexander > Could it be this easy? In one spot you refer to it as "cpu_temperature" and in another as "cputemp". If that's not it, you'd probably better show us your *real* code, otherwise we're just guessing. Gary Herron
[toc] | [prev] | [next] | [standalone]
| From | KarlE <ranstam@gmail.com> |
|---|---|
| Date | 2012-12-22 14:50 -0800 |
| Message-ID | <b611db0c-a500-4423-b9d7-ce569f49c596@googlegroups.com> |
| In reply to | #35381 |
On Saturday, December 22, 2012 9:36:41 PM UTC+1, KarlE wrote: > Hi! > > > > Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email. > > > > The two programs work very well on their own, but this doesnt work. > > > > this works: server.sendmail(fromaddr, toaddrs, msg) > > but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature) > > > > despite the command "print cputemp" working in the same program. > > > > When i run the program i get the error: > > > > Traceback (most recent call last): > > File "sendcpu.py", line 36, in <module> > > msg = cpu_temperature > > NameError: name 'cpu_temperature' is not defined > > > > Does anyone know why the program claims that cpu_temperature isnt defined, when it is? > > > > Thanx! > > > > //Alexander Thanx for the help! After reading your comments i am starting to suspect that i lack basic knowledge of Python programming. I will try to do some reading and undertand what i got my self into!
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-12-23 10:15 +1100 |
| Message-ID | <mailman.1218.1356218145.29569.python-list@python.org> |
| In reply to | #35397 |
On Sun, Dec 23, 2012 at 9:50 AM, KarlE <ranstam@gmail.com> wrote: > Thanx for the help! > > After reading your comments i am starting to suspect that i lack basic knowledge of Python programming. I will try to do some reading and undertand what i got my self into! That happens :) Python has rules that are different from the ones many other languages follow (indentation defining blocks, etc); but it has an excellent tutorial that walks you through all that: http://docs.python.org/3/tutorial/index.html Work through that and you'll be Pythoning with the best in no time! (Disclaimer: "in no time" requires a Faster-Than-Light drive, not included. All guarantees are conditional on functional FTL drive.) ChrisA
[toc] | [prev] | [next] | [standalone]
| From | KarlE <ranstam@gmail.com> |
|---|---|
| Date | 2012-12-23 05:46 -0800 |
| Message-ID | <bf6655e9-4b90-4be7-a5c6-758777ea6c0e@googlegroups.com> |
| In reply to | #35381 |
On Saturday, December 22, 2012 9:36:41 PM UTC+1, KarlE wrote:
> Hi!
>
>
>
> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
>
>
>
> The two programs work very well on their own, but this doesnt work.
>
>
>
> this works: server.sendmail(fromaddr, toaddrs, msg)
>
> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
>
>
>
> despite the command "print cputemp" working in the same program.
>
>
>
> When i run the program i get the error:
>
>
>
> Traceback (most recent call last):
>
> File "sendcpu.py", line 36, in <module>
>
> msg = cpu_temperature
>
> NameError: name 'cpu_temperature' is not defined
>
>
>
> Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
>
>
>
> Thanx!
>
>
>
> //Alexander
Ok, im back with a little more understanding of python! I got the program working, every time my Raspberry Pi reboots i get an Email containing information about the boot and the CPU temperature.
The issue now is that there seems to be a limitation to how long the message string can be, about 32 letters. The code below works well, but when i add more letters to the string "ord" and pass about 32 in size the email comes through emptpy...
I cant find any information about limitations to strings in Python, or the email module. can anyone give me a pointer?
(the code lines my appear with different tabbings due to beeing copied from my raspberry pi with Putty, but this is not an issue, all the lines are on the same tab)
#!/usr/bin/env python
from __future__ import division
from subprocess import PIPE, Popen
import psutil
import smtplib
def get_cpu_temperature():
process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
output, _error = process.communicate()
return float(output[output.index('=') + 1:output.rindex("'")])
def main():
cpu_temperature = get_cpu_temperature()
cpu_usage = psutil.cpu_percent()
ram = psutil.phymem_usage()
ram_percent_used = ram.percent
disk = psutil.disk_usage('/')
disk_percent_used = disk.percent
print 'CPU temperature: ', cpu_temperature
fromaddr = 'xxx'
toaddrs = 'xxx'
username = 'xxx'
password = 'xxx'
ord = "Subject: Pi Boot, CPU: " + str(cpu_temperature)
print len(ord)
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, ord)
server.quit()
main()
[toc] | [prev] | [next] | [standalone]
| From | Mitya Sirenef <msirenef@lightbird.net> |
|---|---|
| Date | 2012-12-23 12:23 -0500 |
| Message-ID | <mailman.1223.1356283409.29569.python-list@python.org> |
| In reply to | #35405 |
On 12/23/2012 08:46 AM, KarlE wrote:
> On Saturday, December 22, 2012 9:36:41 PM UTC+1, KarlE wrote:
>> Hi!
>>
>>
>>
>> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
>>
>>
>>
>> The two programs work very well on their own, but this doesnt work.
>>
>>
>>
>> this works: server.sendmail(fromaddr, toaddrs, msg)
>>
>> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
>>
>>
>>
>> despite the command "print cputemp" working in the same program.
>>
>>
>>
>> When i run the program i get the error:
>>
>>
>>
>> Traceback (most recent call last):
>>
>> File "sendcpu.py", line 36, in <module>
>>
>> msg = cpu_temperature
>>
>> NameError: name 'cpu_temperature' is not defined
>>
>>
>>
>> Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
>>
>>
>>
>> Thanx!
>>
>>
>>
>> //Alexander
> Ok, im back with a little more understanding of python! I got the program working, every time my Raspberry Pi reboots i get an Email containing information about the boot and the CPU temperature.
>
> The issue now is that there seems to be a limitation to how long the message string can be, about 32 letters. The code below works well, but when i add more letters to the string "ord" and pass about 32 in size the email comes through emptpy...
>
> I cant find any information about limitations to strings in Python, or the email module. can anyone give me a pointer?
>
> (the code lines my appear with different tabbings due to beeing copied from my raspberry pi with Putty, but this is not an issue, all the lines are on the same tab)
>
> #!/usr/bin/env python
> from __future__ import division
> from subprocess import PIPE, Popen
> import psutil
> import smtplib
>
> def get_cpu_temperature():
> process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
> output, _error = process.communicate()
> return float(output[output.index('=') + 1:output.rindex("'")])
>
>
> def main():
> cpu_temperature = get_cpu_temperature()
> cpu_usage = psutil.cpu_percent()
>
> ram = psutil.phymem_usage()
> ram_percent_used = ram.percent
>
> disk = psutil.disk_usage('/')
> disk_percent_used = disk.percent
>
> print 'CPU temperature: ', cpu_temperature
>
> fromaddr = 'xxx'
> toaddrs = 'xxx'
> username = 'xxx'
> password = 'xxx'
>
> ord = "Subject: Pi Boot, CPU: " + str(cpu_temperature)
>
> print len(ord)
> server = smtplib.SMTP('smtp.gmail.com:587')
> server.starttls()
> server.login(username,password)
> server.sendmail(fromaddr, toaddrs, ord)
> server.quit()
>
> main()
>
I'm not sure if Raspberry Pi has it, but usually you want to
use the email module, as in example on this page:
http://docs.python.org/2/library/email-examples.html#email-examples
I think what happens is that because your message starts
with 'Subject:', it's interpreted as subject header instead of
an email. You can try adding two newlines after Subject:,
that might help... but using email module is best if possible.
-m
--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-12-23 15:34 -0500 |
| Message-ID | <mailman.1224.1356294905.29569.python-list@python.org> |
| In reply to | #35405 |
On 12/23/2012 08:46 AM, KarlE wrote: > On Saturday, December 22, 2012 9:36:41 PM UTC+1, KarlE wrote: >> Hi! >> >> >> >> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email. >> >> >> >> The two programs work very well on their own, but this doesnt work. >> >> >> >> this works: server.sendmail(fromaddr, toaddrs, msg) >> >> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature) >> >> >> >> despite the command "print cputemp" working in the same program. >> >> >> >> When i run the program i get the error: >> >> >> >> Traceback (most recent call last): >> >> File "sendcpu.py", line 36, in <module> >> >> msg = cpu_temperature >> >> NameError: name 'cpu_temperature' is not defined >> >> >> >> Does anyone know why the program claims that cpu_temperature isnt defined, when it is? >> >> >> >> Thanx! >> >> >> >> //Alexander > Ok, im back with a little more understanding of python! I got the program working, every time my Raspberry Pi reboots i get an Email containing information about the boot and the CPU temperature. > > The issue now is that there seems to be a limitation to how long the message string can be, about 32 letters. The code below works well, but when i add more letters to the string "ord" and pass about 32 in size the email comes through emptpy... I don't know the email protocols that well, but I can tell you a Python string isn't limited in size to any small value. Maybe a few hundred million characters, but i haven't tried beyond that. i suspect the limit you're hitting is the limit of subject size in an email protocol. In particular, the 3rd argument to sendmail() needs to have newlines in a particular format before you get to the body of the email. The body can be quite large, but you probably are required to have the appropriate headers first. When you send a short message, does it all come out as a subject line? It would be good to look up the docs on smtplib, but if I had to just do some blind testing, I'd try first adding a newline pair, changing the line to something like: ord = "Subject: Pi Boot, CPU: \r\n" + str(cpu_temperature) + WhateverOtherStuffYouWantedToTry -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-12-23 19:31 -0500 |
| Message-ID | <mailman.1229.1356309117.29569.python-list@python.org> |
| In reply to | #35405 |
On 12/23/2012 12:23 PM, Mitya Sirenef wrote:
> On 12/23/2012 08:46 AM, KarlE wrote:
>> On Saturday, December 22, 2012 9:36:41 PM UTC+1, KarlE wrote:
>> from __future__ import division
Depending on the linux installed, you should be able to run 3.2 or 3.3
instead of 2.7. Though there are still 2.x only modules, some things
work better in 3.x (including default division).
>> server.sendmail(fromaddr, toaddrs, ord)
>> server.quit()
>>
>> main()
>>
> I'm not sure if Raspberry Pi has it, but usually you want to
> use the email module, as in example on this page:
>
> http://docs.python.org/2/library/email-examples.html#email-examples
3.2+ have a separate .send_message method for email message objects.
> I think what happens is that because your message starts
> with 'Subject:', it's interpreted as subject header instead of
> an email. You can try adding two newlines after Subject:,
> that might help... but using email module is best if possible.
The 3.3 SMTP doc says that the fromaddr and toaddrs are only used by the
transport layer and do not become part of the email message. The doc
example has
msg = ("From: %s\r\nTo: %s\r\n\r\n"
% (fromaddr, ", ".join(toaddrs)))
The body is appended after the double return. A subject line and any
other standard headers would go before.
OT note: the PSF (Python Software Foundation) has bought a Raspberry PI
and another ARM board to test Python on. I am happy to read that it
seems to be working so far.
--
Terry Jan Reedy
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web