неділя, 1 лютого 2015 р.

How to send email with Python help. Smtplib

Lets suppose that you already had working smtp server (like Postfix or Exim) and it works on 25 port. You want to send email using your Python script. I hope this article will help you to do that.

At first make sure that you server can send emails.  Send simple email using bash or shell:

# mail -s “Hello world” test@example.com

And check mailbox test@example.com. Ok, I hope that everything is fine.

Now open your favourite editor and pass there text like that:


середа, 14 січня 2015 р.

Python Code Style

I think it is very important to write clear code if you want that somebody can read and maybe improve it.This is not my advices, you can read original on http://docs.python-guide.org/en/latest/

Check if variable equals a constant

You don’t need to explicitly compare a value to True, or None, or 0 - you can just add it to the if statement. See Truth Value Testing for a list of what is considered false.

Bad:

if attr == True:
    print 'True!'

if attr == None:
    print 'attr is None!'

Good:

# Just check the value
if attr:
    print 'attr is truthy!'

# or check for the opposite
if not attr:
    print 'attr is falsey!'

# or, since None is considered false, explicitly check for it
if attr is None:
    print 'attr is None!'

Access a Dictionary Element

Don’t use the dict.has_key() method. Instead, use x in d syntax, or pass a default argument to dict.get().

Bad:

d = {'hello': 'world'}
if d.has_key('hello'):
    print d['hello']    # prints 'world'
else:
    print 'default_value'

Good:

d = {'hello': 'world'}

print d.get('hello', 'default_value') # prints 'world'
print d.get('thingy', 'default_value') # prints 'default_value'

# Or:
if 'hello' in d:
    print d['hello']