import pymysql
#打开数据库 (如果连接失败会报错)
#db = pymysql.connect(host = '127.0.0.1', port = 3306, user = 'minbo', passwd = '123456', db = 'pythontest')
db = pymysql.connect(host = '127.0.0.1', port = 3306, user = 'minbo', passwd = '123456', db = 'pythontest', charset="utf8")
#获取游标对象
cursor = db.cursor()
#执行sql查询操作
sql_select = "select version()"
cursor.execute(sql_select)
#使用fetchone()获取单条数据
data = cursor.fetchone()
print("DB version is : %s" % data)
#如果user表存在,就删除
cursor.execute("drop table if exists user")
#创建表user
sql_create = "create table user(id int, name varchar(10)) engine = innodb charset = utf8"
cursor.execute(sql_create)
#插入操作
sql_insert = '''insert into user(id, name) values (2, "李明")'''
try:
#执行sql
cursor.execute(sql_insert)
db.commit()
except:
#发生异常
db.rollback()
#查询操作
sql_select = '''select * from user'''
try:
#执行sql语句
cursor.execute(sql_select)
#获取所有记录列表
result = cursor.fetchall()
for row in result:
id = row[0]
name = row[1]
print("id = %d, name = %s" % (id, name))
except:
print("Error: unable to fecth data")
#执行事务
'''事务机制可以确保数据的一致性
1.事务有四个属性:原子,一致,隔离,持久;通常称为ACID
2.Python DB API2.0的事务提供了两个方法:commit 和 rollback
3.对于支持事务的数据库,在python数据库编程中,当游标建立之时,就自动开始了一个隐形的数据库事务,
这个区别于mysql客户端,commit()方法提交所有的事务,rollback()方法回滚当前游标的所有操作。每个方法都开启了一个新的事务'''
#例子
sql_insert = '''insert into test(id, name) values (1, 'china')'''
try:
cursor.execute(sql_insert)
db.commit()
except:
db.rollback()
print("end")
#关闭连接
db.close()
import re
import sys
def getAddress(port):
pattern = r'\S+'
f = open('1.txt')
while True:
data = ''
for line in f:
if line != '\n':
data += line
else:
break
# 表示文档结束
if not data:
break
PORT = re.match(pattern, data).group()
if port == PORT: # 确认对应段
pattern = r'address\s+is\s+(\S+)'
addr = re.search(pattern, data).group(1)
return addr
else:
continue
return data
if __name__ == "__main__":
port = sys.argv[1]
print(getAddress(port))
from threading import Thread
from time import ctime,sleep
# 创建自己的线程类
class MyThread(Thread):
def __init__(self,target,name = "Tedu",args = (),kwargs = {}):
super().__init__()
self.name = name
self.target = target
self.args = args
self.kwargs = kwargs
def run(self):
self.target(*self.args,**self.kwargs)
# 线程事件
def player(song,sec):
for i in range(2):
print("Playing %s : %s" % (song,ctime()))
sleep(sec)
t = MyThread(target = player,args = ('凉凉',3))
#自动执行run()
t.start()
t.join()
from socket import *
from threading import *
HOST = '0.0.0.0'
PORT = 8888
ADDR = (HOST,PORT)
#创建套接字
s = socket()
s.bind(ADDR)
s.listen(5)
def handler(connfd):
print("Got connetion from",connfd.getpeername())
while True:
data = connfd.recv(1024).decode()
if not data:
break
connfd.send(b'receive your message')
connfd.close()
while True:
try:
connfd,addr = s.accept()
except KeyboardInterrupt:
s.close()
sys.exit("服务器退出")
except Exception as e
print(e)
continue
t = Thread(target = handler,args = (connfd,))
t.setDaemon(True)
t.start()
def dec1(func):
print("1111")
def one():
print("2222")
func()
print("3333")
return one
def dec2(func):
print("aaaa")
def two():
print("bbbb")
func()
print("cccc")
return two
@dec1
@dec2
def test():
print("test test")
test()