[Tutor] sending mails from python
I'm trying to learn how to send e-mails with python. But I always become this error: socket.error: (110, 'Connection timed out'). Is there something bad in the code? Does anyone knows what could be wrong? I'm using python from linux, and have no proxy... The code that I use is this (of course, filling with my persona data the " "): import smtplib smtpserver = 'my smtp server goes here' AUTHREQUIRED = 1 smtpuser = 'my user name' smtppass = 'my password' RECIPIENTS = ['the recipients'] SENDER = 'my mail' mssg = "una prueba desde python" session = smtplib.SMTP(smtpserver) if AUTHREQUIRED: session.login(smtpuser, smtppass) smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg) if smtpresult: errstr = "" for recip in smtpresult.keys(): errstr = """Could not delivery mail to: %s Server said: %s %s %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr) raise smtplib.SMTPException, errstr Thank you for your help __ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] checking for robots on a python script based site
Hello All!I have a site that takes some input from the user and then produces a dynamic page based on the input. In the last months bots (not only search engine ones) have been hitting the site pretty heavily, so my sys admin disabled it. As far as I know there are 2 options to fight this : 1: write a robots.txt file - but then I'd have to rely on the bots' good will.2: implement a "captcha" mechanism, which I am not sure I want. Does Python provide anything to deal with such problems ? thanks, Tsila ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Best way to replace items in a list.
Jason Massey wrote: > Why not: > > if item in lst: > loc = lst.index(item) > lst[loc] = str You can also just try to do the replacement and catch the ValueError that is raised if the item is not there: try: loc = list.index(item) list[loc] = str except ValueError: pass If lst is long this will be faster because it only searches lst for item once. If lst is short maybe the first version is better because the code is shorter. This is an example of two different approaches to handling exceptional conditions: Look Before You Leap versus Easier to Ask Forgiveness than Permission. In LBYL you check for the exceptional condition early, so you avoid exceptions. In EAFP you proceed as if something is sure to work and clean up after if you were wrong. In many cases EAFP produces code that makes fewer assumptions about its environment and is more suited to Python's dynamic style. In this simple case it doesn't make any difference. By the way don't use 'list' as the name of a variable (or 'file', 'dict', 'set' or 'str'), they are all the names of built-in types in Python and using them as variable names will hide the built-in name. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sending mails from python
On Sat, 2006-10-21 at 12:45 +0200, euoar wrote: > I'm trying to learn how to send e-mails with python. But I always become > this error: socket.error: (110, 'Connection timed out'). It sounds like you were unable to connect to the mail server. That could be due to a large number of network issues and is probably not a Python bug. from a terminal/cmd window try telnet smtpserver 25 That will connect to the smtpserver on the proper port (and works for most operating systems). I expect that to fail. Your Python code will not work until you can actually make a connection. Here's what I get trying to reach a non-existent smtpserver: telnet 10.10.10.10 25 Trying 10.10.10.10... telnet: connect to address 10.10.10.10: Connection timed out telnet: Unable to connect to remote host: Connection timed out > Is there > something bad in the code? Does anyone knows what could be wrong? I'm > using python from linux, and have no proxy... > > The code that I use is this (of course, filling with my persona data the > " "): > > import smtplib > > smtpserver = 'my smtp server goes here' > AUTHREQUIRED = 1 > smtpuser = 'my user name' > smtppass = 'my password' > > RECIPIENTS = ['the recipients'] > SENDER = 'my mail' > mssg = "una prueba desde python" > > session = smtplib.SMTP(smtpserver) > if AUTHREQUIRED: > session.login(smtpuser, smtppass) > smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg) > > if smtpresult: > errstr = "" > for recip in smtpresult.keys(): > errstr = """Could not delivery mail to: %s > > Server said: %s > %s > > %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr) > raise smtplib.SMTPException, errstr > > > > Thank you for your help > > > > > > > __ > LLama Gratis a cualquier PC del Mundo. > Llamadas a fijos y móviles desde 1 céntimo por minuto. > http://es.voice.yahoo.com > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Lloyd Kvam Venix Corp ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sending mails from python
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 How about something like this? Create a function and then call it with the required optins of server url, sender address, destination address, subject, and message text? def send(serverURL=None, sender='', to='', subject='', text=''): """ Form and send an e-mail message """ import smtplib headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, to, subject) message = headers + text mailServer = smtplib.SMTP(serverURL) # replace serverURL with site specific mail server check_valid=mailer.isAddressValid(to) if check_valid==1 : mailServer.sendmail(sender, to, message) mailServer.quit() else: print "\n\nDestination e-mail address %s is invalid. \n\n\tPlease check and run again" % to def isAddressValid(addr): """ Validate e-mail addresses based on lexical rules set forth in RFC 822 Ported from Recipe 3.9 in Secure Programming Cookbook for C and C++ by John Viega and Matt Messier (O'Reilly 2003) If the supplied email address is syntactically valid, isAddressValid() will return 1; otherwise, it will return 0. """ # Mail variables rfc822_specials = '()<>@,;:\\"[]' c = 0 while c < len(addr): if addr[c] == '"' and (not c or addr[c - 1] == '.' or addr[c - - 1] == '"'): c = c + 1 while c < len(addr): if addr[c] == '"': break if addr[c] == '\\' and addr[c + 1] == ' ': c = c + 2 continue if ord(addr[c]) < 32 or ord(addr[c]) >= 127: return 0 c = c + 1 else: return 0 if addr[c] == '@': break if addr[c] != '.': return 0 c = c + 1 continue if addr[c] == '@': break if ord(addr[c]) <= 32 or ord(addr[c]) >= 127: return 0 if addr[c] in rfc822_specials: return 0 c = c + 1 if not c or addr[c - 1] == '.': return 0 # Next we validate the domain portion ([EMAIL PROTECTED]) domain = c = c + 1 if domain >= len(addr): return 0 count = 0 while c < len(addr): if addr[c] == '.': if c == domain or addr[c - 1] == '.': return 0 count = count + 1 if ord(addr[c]) <= 32 or ord(addr[c]) >= 127: return 0 if addr[c] in rfc822_specials: return 0 c = c + 1 return count >= 1 euoar wrote: > I'm trying to learn how to send e-mails with python. But I always become > this error: socket.error: (110, 'Connection timed out'). Is there > something bad in the code? Does anyone knows what could be wrong? I'm > using python from linux, and have no proxy... > > The code that I use is this (of course, filling with my persona data the > " "): > > import smtplib > > smtpserver = 'my smtp server goes here' > AUTHREQUIRED = 1 > smtpuser = 'my user name' > smtppass = 'my password' > > RECIPIENTS = ['the recipients'] > SENDER = 'my mail' > mssg = "una prueba desde python" > > session = smtplib.SMTP(smtpserver) > if AUTHREQUIRED: > session.login(smtpuser, smtppass) > smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg) > > if smtpresult: > errstr = "" > for recip in smtpresult.keys(): > errstr = """Could not delivery mail to: %s > > Server said: %s > %s > > %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr) > raise smtplib.SMTPException, errstr > > > > Thank you for your help > > > > > > > __ > LLama Gratis a cualquier PC del Mundo. > Llamadas a fijos y móviles desde 1 céntimo por minuto. > http://es.voice.yahoo.com > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -BEGIN PGP SIGNATURE- Version: GnuPG v1.2.1 (MingW32) Comment: GnuPT 2.7.2 iD8DBQFFOhCLDvn/4H0LjDwRAm9AAJwOkrtF/R4zWQhhCwjlBrBVQ4XpwwCgpGHy ukZIoIuoAh/hmBZ/r/ZbQmM= =6weK -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] what is PIL..???
Folks, Continuing my journey into "Tkinter world"... May I ask someone to tell what PIL is and how can it be helpful in drawing 2-dimensional graphs... Thanks.. Regards, Asrarahmed-- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] what is PIL..???
On 10/21/06, Asrarahmed Kadri <[EMAIL PROTECTED]> wrote: > May I ask someone to tell what PIL is and how can it be helpful in drawing > 2-dimensional graphs... PIL = Python Imaging Library PIL provides a bunch of nifty tools for handling images and is well worth a google. -Rob A. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyAlsaAudio with Multiple Sound Cards?
On Fri, 20 Oct 2006 18:13:04 -0400 "Rick Sterling" <[EMAIL PROTECTED]> wrote: > > Hi. > > I am pretty new to Python, but am trying to get up to speed so I move over > to Python from Perl. One progam I wrote in Perl I am trying to re-write in > Python. It controls the mixer settings on my sound card. I managed to > rewrite most of it by borrowing and stealing from the mixertest.py included > with PyAlsaAudio. > > I haven't been able to figure out on thing however. I know that normally > when you assign the mixer with: > > mixer = alsaaudio.Mixer("Master") > > It seems to use the first card in the system. My system has two sound > cards, and I can't figure out how to access the second card. Digging around > online I found the following syntax: > > mixdev = alsaaudio.Mixer(mixer, id, device) > > I don't know what values it expects for id and device. I have tried using 0 > for the id and using "hw:1" for the device to no avail. I am sure it is > something simple, but after trying many different things I still haven't > been able to figure out how to access my second sound card. > Hi Rick, isn't there any documentation for pyalsaaudio? If no, maybe you would be better off using ossaudiodev, which works well with ALSA, too. For the device question, I guess they might want something like device=/dev/snd/hwC0D0 or however these device files are named on your system. I hope this helps Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] what is PIL..???
Asrarahmed Kadri wrote: > > Folks, > > Continuing my journey into "Tkinter world"... > > May I ask someone to tell what PIL is and how can it be helpful in > drawing 2-dimensional graphs... Python Imaging Library - just Google PIL. I don't think it will help much with graphs though, if you mean things like pie charts and bar graphs. For that you might look at matplotlib or one of the other packages listed here: http://wiki.python.org/moin/NumericAndScientific/Plotting Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] what is PIL..???
Yeah, I am looling for bar graphs On 10/21/06, Kent Johnson <[EMAIL PROTECTED]> wrote: Asrarahmed Kadri wrote:>> Folks,>> Continuing my journey into "Tkinter world"... >> May I ask someone to tell what PIL is and how can it be helpful in> drawing 2-dimensional graphs...Python Imaging Library - just Google PIL.I don't think it will help much with graphs though, if you mean things like pie charts and bar graphs. For that you might look at matplotlib orone of the other packages listed here:http://wiki.python.org/moin/NumericAndScientific/Plotting Kent___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor -- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] what is PIL..???
"Asrarahmed Kadri" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Yeah, I am looling for bar graphs > If its simple 2D bar graphs they are almost as easy to draw yourself on a Canvas widget using coloured rectangles. The only trickiness is in drawing axes and scales. But its not rocket science. But if you want to get into power features like stacked graphs, 3D boxes, and other graph types a charting/plotting library will be better even given the extra learning curve. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] checking for robots on a python script based site
On Sat, 21 Oct 2006, Tsila Hassine wrote: > 1: write a robots.txt file - but then I'd have to rely on the bots' good > will. > 2: implement a "captcha" mechanism, which I am not sure I want. > > Does Python provide anything to deal with such problems ? I think you're asking the question: "Has anyone written a system in Python that tries to deal with this problem?" You may want to look at: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440588 Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] what is PIL..???
Also checkout GnuPlot http://www.gnuplot.info/and the Python bindings for ithttp://gnuplot-py.sourceforge.net/ Date: Sat, 21 Oct 2006 14:07:12 +0100From: "Asrarahmed Kadri" <[EMAIL PROTECTED]> Subject: [Tutor] what is PIL..???To: pythontutorMessage-ID: < [EMAIL PROTECTED]>Content-Type: text/plain; charset="iso-8859-1"Folks,Continuing my journey into "Tkinter world"...May I ask someone to tell what PIL is and how can it be helpful in drawing 2-dimensional graphs...Thanks.. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] checking for robots on a python script based site
Tsila Hassine wrote: > Hello All! > > I have a site that takes some input from the user and then produces a > dynamic page based on the input. In the last months bots (not only > search engine ones) have been hitting the site pretty heavily, so my sys > admin disabled it. As far as I know there are 2 options to fight this : > 1: write a robots.txt file - but then I'd have to rely on the bots' good > will. Seems to me this is worth doing, at least you will screen out the white hat robots. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] can i pass a list to a function and get one back ?
hey there, i was just wondering if i could get a list back from a function.something likedef return_a_list(some_var): some_list = [] for i in range(5): var = some_var + i some_list.append(var) return some_listis this cool ?thanks ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] can i pass a list to a function and get one back ?
On Sat, Oct 21, 2006, shawn bright wrote: > > hey there, i was just wondering if i could get a list back from a > function. > something like > def return_a_list(some_var): > some_list = [] > for i in range(5): > var = some_var + i > some_list.append(var) > return some_list > is this cool ? Ayup! The best way to find answers to questions like this is to just do it, and see what python does. Bill -- INTERNET: [EMAIL PROTECTED] Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way FAX:(206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676 ``It will be of little avail to the people that the laws are made by men of their own choice if the laws be so voluminous that they cannot be read, or so incoherent that they cannot be understood.'' -James Madison, Federalist Paper #62 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Exception and sys.exit() in a cgi script
Mike Hansen Mike.Hansen at atmel.com Mon Oct 16 18:43:29 CEST 2006 > This is a peace of a CGI script i have. > > 1 import cgi > 2 form=cgi.FieldStorage() > 3 try : > 4 ano=form["ano"].value > 5 conta=form["conta"].value > 6 except KeyError : > 7 print 'Please enter values in the > fields ' > 8 sys.exit(0) > > > When the excption occurs, no message is shown on the browser. > > If I run the script with IDLE, the message is printed and then the > script exits. > > What's wrong here? > > > > ___ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > I'm catching up on the weekend's tutor messages. import cgitb; cgitb.enable() That spits errors to the web page which is great for debugging cgi scripts. I think you need print "Content-Type: text/html\n\n" http://docs.python.org/lib/cgi-intro.html http://docs.python.org/lib/node560.html Mike Thats not the point (sorry). when all the values are provided in the form (no excetion is raised) it runs as expected! The point is when one of the values "ano", "conta" is missing in the form, I want the error message to be shown on the browser, and the script to stop running. When there is an exception, the script actually stops running, but no message is sent to the browser. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] can i pass a list to a function and get one back ?
great, thanks, i have idle right here, would have been just as easy.sorry about thatskOn 10/21/06, Bill Campbell < [EMAIL PROTECTED]> wrote:On Sat, Oct 21, 2006, shawn bright wrote:> > hey there, i was just wondering if i could get a list back from a> function.> something like> def return_a_list(some_var):> some_list = []> for i in range(5): > var = some_var + i> some_list.append(var)> return some_list> is this cool ?Ayup!The best way to find answers to questions like this is to just doit, and see what python does. Bill--INTERNET: [EMAIL PROTECTED] Bill Campbell; Celestial Software LLCURL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way FAX:(206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676``It will be of little avail to the people that the laws are made by men oftheir own choice if the laws be so voluminous that they cannot be read, or so incoherent that they cannot be understood.''-James Madison, Federalist Paper #62___Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] can i pass a list to a function and get one back ?
> great, thanks, i have idle right here, would have been just as easy. > sorry about that No problem; it's cool. >> > hey there, i was just wondering if i could get a list back from a >> > function. >> > something like >> > def return_a_list(some_var): >> > some_list = [] >> > for i in range(5): >> > var = some_var + i >> > some_list.append(var) >> > return some_list >> > is this cool ? It's cool. *grin* One small comment: we often don't need to use temporary variables like 'var'. def return_a_list(some_var): some_list = [] for i in range(5): some_list.append(some_var + i) return some_list Sometimes a temporary variable is useful, and sometimes not. Here, it seems like it's not too necessary. Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] 'json' should probably be the response to 'can we use eval to parse { ... }?'
Hi everyone, I ran across the JSON data format today: http://json.org/ with a nice Python implementation in 'simplejson'. http://undefined.org/python/#simple_json The JSON format matches very closely with Python data structure syntax. So the next time someone asks if they can use eval() to turn a string into a data structure, let's point them to simplejson. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] can i pass a list to a function and get one back ?
way cool, thanks. I am always looking for ways to clean things up-skOn 10/21/06, Danny Yoo <[EMAIL PROTECTED] > wrote:> great, thanks, i have idle right here, would have been just as easy. > sorry about thatNo problem; it's cool.>> > hey there, i was just wondering if i could get a list back from a>> > function.>> > something like>> > def return_a_list(some_var): >> > some_list = []>> > for i in range(5):>> > var = some_var + i>> > some_list.append(var)>> > return some_list>> > is this cool ? It's cool. *grin*One small comment: we often don't need to use temporary variables like'var'.def return_a_list(some_var):some_list = []for i in range(5):some_list.append(some_var + i) return some_listSometimes a temporary variable is useful, and sometimes not. Here, itseems like it's not too necessary.Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] 5 questions
1. Is there a searchable archive of this list? 2. Is there list ettiquette one should be aware of? 3. Besides Vaults of Parnassus, are there other webpages which list projects written in Python? I'm mainly interested in looking at non-technical software, things for the common user to use. I've been surprised at just how little of that type I've found so far. 4. Can anyone recommend good books for non-programmers starting out learning Python? I've looked through a few so far and they are ok ("Learning Python", "Python: How to Program") but am still hoping to find one that doesn't assume any prior knowledge of programming and defines terms when they are first presented. (That might be a tall order, I know...) 5. If I want to take user information from text boxes, check boxes, etc., and store it for later re-display to the person (imagine a movie rating diary, with text review of the film and checkboxes or sliders to give it 5 stars, or has action, or is a comedy, etc.) and have all of it be efficiently searchable, what are some good ways to go in terms of how to save the information? For now I have textbox info saving just to a .txt file, but that can't be a good way to go about it. I have the vague sense I should learn about databases, but really am not sure what to read about... Suggestions? _ All-in-one security and maintenance for your PC. Get a free 90-day trial! http://clk.atdmt.com/MSN/go/msnnkwlo005002msn/direct/01/?href=http://www.windowsonecare.com/?sc_cid=msn_hotmail ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor