Python每日一練(1)


PythonsocketPILpymysqlFlask

socket

socket

serverclient

server

1ip

2

client

1

2


 

PILPIL:

1

00

 1 # -*- coding:utf-8 -*-
 2 # 每日一練:將你的 QQ 頭像(或者微博頭像)右上角加上紅色的數字
 3 # 類似於微信未讀信息數量那種提示效果。
 4 # Author : Konmu
 5 
 6 from PIL import Image,ImageFont,ImageDraw
 7 
 8 def Image_fill(im_file,num):
 9     im=Image.open(im_file)
10     width,height=im.size
11     font_type=ImageFont.truetype('C:/Windows/fonts/字酷堂海藏楷體.ttf',50)
12     fill_color='steelblue'
13     draw=ImageDraw.ImageDraw(im)
14     draw.text((width-50,0),str(num),fill=fill_color,font=font_type)
15     #圖片坐標左上角開始為原點,平移符合了左加右減原則
16     #故到達右上角即減去一個偏移量即可
17     save_file='C:/Users/xxx/Desktop/output.jpg'
18     im.save(save_file)
19 
20 if __name__=='__main__':
21     Image_fill('C:/Users/xxx/Desktop/少天.jpg',21)


 

20mysqlpython

pythonpymysqlmysql

mysql

1 create database test; #創建數據庫
2 create table gencode(id int auto_increment primary key,value varchar(20)); #創建數據表,id 是采用的自增長型
3 insert into gencode(id,value) values (1,'GB0XRF2boYF2BPEp46l5') #向表中插入數據,這里要注意數據的類型
4 drop table if exists test;  #刪除已存在的表

pymysql

1 db=pymysql.connect('localhost','user','password','table') #連接本地數據庫,其中用戶,密碼和數據庫填寫自己的信息即可
2 cursor=db.cursor() #創建cursor對象
3 cursor.execute(sql) #執行相關sql語句
4 db.commit() #提交數據到數據庫
5 db.close() #關閉數據庫

 1 #!/user/bin/python3
 2 #-*-coding:utf-8 *-*
 3 #Author:konmu
 4 #生成激活碼(或者優惠券),使用 Python 如何生成20個激活碼(或者優惠券)?將生成的激活碼(或者優惠券)保存到 MySQL 關系型數據庫中。
 5 
 6 import pymysql
 7 import random,string
 8 
 9 def genkey():
10     getChars = string.ascii_letters+string.digits
11     generate =  "".join([random.choice(getChars)for i in range(20)])
12     return(generate)
13 
14 def table_insert(cursor, db):
15     for i in range(20):
16         sql = "insert into gencode (id,value) values ({0},'{1}')".format("null", genkey())#向表中插入數據
17         cursor.execute(sql)
18         db.commit()
19         
20 if __name__=='__main__':
21     db = pymysql.connect("localhost", "root", "123456", "test")#本次測試使用的數據庫是test
22     cursor = db.cursor()#創建cursor對象
23     cursor.execute("drop table if exists gencode")#刪除gencode表如果已存在的話
24     sql = "create table generateCodes(id int auto_increment primary key, value varchar(50))"#創建表
25     try:
26         cursor.execute(sql)
27         db.commit()
28     except:
29         db.rollback()
30     genkey()
31     table_insert(cursor, db)
32     db.close()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM