==================================
#!/usr/bin/python
#-*-Coding:utf-8-*-
Import MySQLdb
# Open Database connection
db = MySQLdb.connect ("localhost", "testuser", "test123", "TESTDB", charset= ' UTF8 ')
# get an action cursor using the cursor () method
cursor = Db.cursor ()
# If the data table already exists, use the Execute () method to delete the table.
Cursor.execute ("DROP TABLE IF EXISTS EMPLOYEE")
# Create a data table SQL statement
sql = "" "CREATE TABLE EMPLOYEE (
First_Name CHAR () not NULL,
Last_Name CHAR (20),
Age INT,
SEX CHAR (1),
INCOME FLOAT) "" "
Cursor.execute (SQL)
# Close the database connection
Db.close ()
----------------------------------
#!/usr/bin/python
#-*-Coding:utf-8-*-
Import MySQLdb
# Open Database connection
db = MySQLdb.connect ("localhost", "testuser", "test123", "TESTDB", charset= ' UTF8 ')
# get an action cursor using the cursor () method
cursor = Db.cursor ()
# SQL UPDATE statement
sql = "UPDATE EMPLOYEE SET age = age + 1 WHERE SEX = '%c '"% (' M ')
Try
# Execute SQL statement
Cursor.execute (SQL)
# Commit to Database execution
Db.commit ()
Except
# Roll Back when an error occurs
Db.rollback ()
# Close the database connection
Db.close ()
-------------------------------------------------------------
#!/usr/bin/python
#-*-Coding:utf-8-*-
Import MySQLdb
# Open Database connection
db = MySQLdb.connect ("localhost", "testuser", "test123", "TESTDB", charset= ' UTF8 ')
# get an action cursor using the cursor () method
cursor = Db.cursor ()
# SQL Query Statement
sql = "SELECT * from EMPLOYEE \
WHERE INCOME > '%d ' "% (1000)
Try
# Execute SQL statement
Cursor.execute (SQL)
# Get a list of all records
Results = Cursor.fetchall ()
For row in results:
fname = row[0]
LName = row[1]
Age = Row[2]
Sex = row[3]
Income = Row[4]
# Print Results
Print "fname=%s,lname=%s,age=%d,sex=%s,income=%d"% \
(FName, lname, age, sex, income)
Except
Print "error:unable to FECTH data"
# Close the database connection
Db.close ()
======================================================
#!/usr/bin/python
#-*-Coding:utf-8-*-
# File Name: server.py
Import Socket # Importing Socket module
s = Socket.socket () # Create socket Object
Host = Socket.gethostname () # Gets the local host name
Port = 80808 # setting ports
S.bind ((host, Port)) # bound Port
S.listen (5) # Waiting for client to connect
While True:
C, addr = S.accept () # establishes a client connection.
print ' Connection address: ', addr
C.send (' Welcome to visit! ')
C.close () # Close connection
-------------------------------------------------
#!/usr/bin/python
#-*-Coding:utf-8-*-
# File Name: client.py
Import Socket # Importing Socket module
s = Socket.socket () # Create socket Object
Host = Socket.gethostname () # Gets the local host name
Port = 808080 # setting Ports good
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 = ' [email protected] '
receivers = [' [email protected] '] # receive mail, can be set as your QQ mailbox or other mailbox
Msgroot = Mimemultipart (' related ')
msgroot[' from ' = Header ("Test Tutorial", ' Utf-8 ')
msgroot[' to ' = Header ("Test", ' Utf-8 ')
Subject = ' Python SMTP mail test '
msgroot[' Subject ' = Header (Subject, ' utf-8 ')
msgalternative = Mimemultipart (' alternative ')
Msgroot.attach (msgalternative)
Mail_msg = "" "
<p>python Mail Send Test ...</p>
<p><a href= "http://www.runoob.com" > Test Tutorials Links </a></p>
<p> Photo Demo:</p>
<p></p>
"""
Msgalternative.attach (Mimetext (mail_msg, ' html ', ' Utf-8 '))
# Specify picture as current directory
fp = open (' test.png ', ' RB ')
Msgimage = Mimeimage (Fp.read ())
Fp.close ()
# define image ID, reference in HTML text
Msgimage.add_header (' Content-id ', ' <image1> ')
Msgroot.attach (Msgimage)
Try
Smtpobj = Smtplib. SMTP (' localhost ')
Smtpobj.sendmail (sender, Receivers, msgroot.as_string ())
Print "Mail sent successfully"
Except Smtplib. Smtpexception:
Print "Error: unable to send mail"
------------------------------------------------------------
#!/usr/bin/python
#-*-Coding:utf-8-*-
Import Smtplib
From Email.mime.text import Mimetext
From email.utils import formataddr
my_sender= ' [email protected] ' # Sender email account
My_pass = ' xxxxxxxxxx ' # sender's mailbox password
my_user= ' [email protected] ' # Recipient email account, my side to send to myself
Def Mail ():
Ret=true
Try
Msg=mimetext (' Fill in message contents ', ' plain ', ' utf-8 ')
msg[' from ']=formataddr (["Fromrunoob", My_sender]) # corresponding sender's mailbox nickname in parentheses, sender's email account
msg[' to ']=formataddr (["FK", My_user]) # corresponding recipient mailbox nickname in parentheses, Recipient's email account
msg[' Subject ']= "tutorial send mail Test" # The subject of the message, also can be said to be the title
Server=smtplib. Smtp_ssl ("smtp.qq.com", 465) # SMTP server in the sender's mailbox, port is 25
Server.login (My_sender, My_pass) # in parentheses corresponds to the sender's mailbox account, email password
Server.sendmail (My_sender,[my_user,],msg.as_string ()) # in parentheses corresponds to the sender's email account, the recipient's email account, and the email
Server.quit () # Close connection
Except Exception: # If the statement in the try is not executed, the following ret=false is executed
Ret=false
return ret
Ret=mail ()
IF RET:
Print ("Mail sent successfully")
Else
Print ("message sent Failed")
===================
Python Database Network mailbox