Python

来自刘东华的维基
跳转至: 导航搜索

概述

Python.

安装配置

安装module

  1. 方法一:yum直接安装,如果有的话
  2. 方法二:用easy_install安装即可,如果有的话
  3. 方法三:下载对应module源码,然后执行pythonX.X setup.py install

用法

日期时间

# format: "yyyy-mm-dd hh:mm:ss"
def current_time():
    return time.strftime("%Y-%m-%d %H:%M:%S")

Mysql

参考: http://mysql-python.sourceforge.net/MySQLdb.html

多线程

check_thread = Thread(target=thread_func)
check_thread.start()

ftp服务器

# ftp server
 
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
 
authorizer = DummyAuthorizer()
authorizer.add_user("fu1", "12345", "/root", perm="elradfmw")
authorizer.add_anonymous("/home/nobody")
 
handler = FTPHandler
handler.authorizer = authorizer
 
server = FTPServer(("0.0.0.0", 21), handler)
server.serve_forever()
# ftp client
 
from ftplib import FTP
 
ftp = FTP('127.0.0.1')
ftp.login('fu1', '12345')
#ftp.retrlines('LIST')
ftp.retrlines('LIST test')

发送邮件

下面例子引用自:http://www.tutorialspoint.com/python/python_sending_email.htm

Example

#!/usr/bin/python
 
import smtplib
 
sender = 'from@fromdomain.com'
receivers = ['to@todomain.com']
 
message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
Subject: SMTP e-mail test
 
This is a test e-mail message.
"""
 
try:
   smtpObj = smtplib.SMTP('smtp.example.com')
   smtpObj.ehlo()
   smtpObj.starttls()
   smtpObj.ehlo()
   smtpObj.login(sender, password)
   smtpObj.sendmail(sender, receivers, message) 
   smtpObj.quit()        
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

Sending an HTML e-mail using Python

参考这个会好些:http://baiying.blog.51cto.com/1068039/1185855

#!/usr/bin/python
 
import smtplib
 
message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test
 
This is an e-mail message to be sent in HTML format
 
<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""
 
try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

Sending Attachments as an E-mail

#!/usr/bin/python
 
import smtplib
import base64
 
filename = "/tmp/test.txt"
 
# Read a file and encode it into base64 format
fo = open(filename, "rb")
filecontent = fo.read()
encodedcontent = base64.b64encode(filecontent)  # base64
 
sender = 'webmaster@tutorialpoint.com'
reciever = 'amrood.admin@gmail.com'
 
marker = "AUNIQUEMARKER"
 
body ="""
This is a test email to send an attachement.
"""
# Define the main headers.
part1 = """From: From Person <me@fromdomain.net>
To: To Person <amrood.admin@gmail.com>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)
 
# Define the message action
part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit
 
%s
--%s
""" % (body,marker)
 
# Define the attachment section
part3 = """Content-Type: multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=%s
 
%s
--%s--
""" %(filename, filename, encodedcontent, marker)
message = part1 + part2 + part3
 
try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, reciever, message)
   print "Successfully sent email"
except Exception:
   print "Error: unable to send email"