三月阳光 发表于 2018-8-9 13:02:32

python数据库网络邮箱

  ==================================
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  import MySQLdb
  # 打开数据库连接
  db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB", charset='utf8' )
  # 使用cursor()方法获取操作游标
  cursor = db.cursor()
  # 如果数据表已经存在使用 execute() 方法删除表。
  cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
  # 创建数据表SQL语句
  sql = """CREATE TABLE EMPLOYEE (
  FIRST_NAMECHAR(20) NOT NULL,
  LAST_NAMECHAR(20),
  AGE INT,
  SEX CHAR(1),
  INCOME FLOAT )"""
  cursor.execute(sql)
  # 关闭数据库连接
  db.close()
  ----------------------------------
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  import MySQLdb
  # 打开数据库连接
  db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB", charset='utf8' )
  # 使用cursor()方法获取操作游标
  cursor = db.cursor()
  # SQL 更新语句
  sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')
  try:
  # 执行SQL语句
  cursor.execute(sql)
  # 提交到数据库执行
  db.commit()
  except:
  # 发生错误时回滚
  db.rollback()
  # 关闭数据库连接
  db.close()
  -------------------------------------------------------------
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  import MySQLdb
  # 打开数据库连接
  db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB", charset='utf8' )
  # 使用cursor()方法获取操作游标
  cursor = db.cursor()
  # SQL 查询语句
  sql = "SELECT * FROM EMPLOYEE \
  WHERE INCOME > '%d'" % (1000)
  try:
  # 执行SQL语句
  cursor.execute(sql)
  # 获取所有记录列表
  results = cursor.fetchall()
  for row in results:
  fname = row
  lname = row
  age = row
  sex = row
  income = row
  # 打印结果
  print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
  (fname, lname, age, sex, income )
  except:
  print "Error: unable to fecth data"
  # 关闭数据库连接
  db.close()
  ======================================================
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  # 文件名:server.py
  import socket               # 导入 socket 模块
  s = socket.socket()         # 创建 socket 对象
  host = socket.gethostname() # 获取本地主机名
  port = 80808                # 设置端口
  s.bind((host, port))      # 绑定端口
  s.listen(5)               # 等待客户端连接
  while True:
  c, addr = s.accept()   # 建立客户端连接。
  print '连接地址:', addr
  c.send('欢迎访问!')
  c.close()                # 关闭连接
  -------------------------------------------------
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  # 文件名:client.py
  import socket               # 导入 socket 模块
  s = socket.socket()         # 创建 socket 对象
  host = socket.gethostname() # 获取本地主机名
  port = 808080               # 设置端口好
  s.connect((host, port))
  print s.recv(1024)
  s.close()
  =================================================
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  import smtplib
  from email.mime.image import MIMEImage
  from email.mime.multipart import MIMEMultipart
  from email.mime.text import MIMEText
  from email.header import Header
  sender = 'from@runoob.com'
  receivers = ['45245751@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
  msgRoot = MIMEMultipart('related')
  msgRoot['From'] = Header("测试教程", 'utf-8')
  msgRoot['To'] = Header("测试", 'utf-8')
  subject = 'Python SMTP 邮件测试'
  msgRoot['Subject'] = Header(subject, 'utf-8')
  msgAlternative = MIMEMultipart('alternative')
  msgRoot.attach(msgAlternative)
  mail_msg = """
  <p>Python 邮件发送测试...</p>
  <p><a href=&quot;http://www.runoob.com&quot;>测试教程链接</a></p>
  <p>图片演示:</p>
  <p><img src=&quot;cid:image1&quot;></p>
  &quot;&quot;&quot;
  msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))
  # 指定图片为当前目录
  fp = open('test.png', 'rb')
  msgImage = MIMEImage(fp.read())
  fp.close()

  # 定义图片>  msgImage.add_header('Content-ID', '<image1>')
  msgRoot.attach(msgImage)
  try:
  smtpObj = smtplib.SMTP('localhost')
  smtpObj.sendmail(sender, receivers, msgRoot.as_string())
  print &quot;邮件发送成功&quot;
  except smtplib.SMTPException:
  print &quot;Error: 无法发送邮件&quot;
  ------------------------------------------------------------
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  import smtplib
  from email.mime.text import MIMEText
  from email.utils import formataddr
  my_sender='568297876@qq.com' # 发件人邮箱账号
  my_pass = 'xxxxxxxxxx' # 发件人邮箱密码
  my_user='568297876@qq.com' # 收件人邮箱账号,我这边发送给自己
  def mail():
  ret=True
  try:
  msg=MIMEText('填写邮件内容','plain','utf-8')
  msg['From']=formataddr([&quot;FromRunoob&quot;,my_sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
  msg['To']=formataddr([&quot;FK&quot;,my_user]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号
  msg['Subject']=&quot;教程发送邮件测试&quot; # 邮件的主题,也可以说是标题
  server=smtplib.SMTP_SSL(&quot;smtp.qq.com&quot;, 465) # 发件人邮箱中的SMTP服务器,端口是25
  server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
  server.sendmail(my_sender,,msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
  server.quit() # 关闭连接
  except Exception: # 如果 try 中的语句没有执行,则会执行下面的 ret=False
  ret=False
  return ret
  ret=mail()
  if ret:
  print(&quot;邮件发送成功&quot;)
  else:
  print(&quot;邮件发送失败&quot;)
  ===================
页: [1]
查看完整版本: python数据库网络邮箱