標籤:creat 對象 擷取 迴圈 爬蟲 not comm inpu transport
1. IO多工
2. paramiko
3. MySQL - SQL語句 - Python操作MySQL - pymysql模組
上節回顧:
- 線程 - 鎖 - 池
- 進程 - 鎖 - 池 - 資料共用
- 協程 - 對線程的分區(微線程) - IO多工
r,w,e = select.select([socket對象,...],[],[],1) - 同時監聽多個socket對象,是否:可讀、可寫、異常 今日內容:
1. IO多工 - 監聽多個socket變化 - socket服務端 IO多工 + socket 來實現Web伺服器
a. 服務端優先運行
b. 瀏覽器: http://...... socket + send(‘協議..‘)
c. 擷取用戶端發來的url,根據URL不同響應資料
d. 取消連結
產出:
a. 瀏覽器發送資料:按照指定規則
b. 監聽多個socket對象 、
c. Web架構開發人員 業務開發人員
d. 模組獨立化 參考部落格:http://www.cnblogs.com/wupeiqi/p/6536518.html - socket用戶端(爬蟲) 利用一個線程,同時發送n個請求(非同步非阻塞模組)
url_list [ http://www.baidu.com, http://www.baidu1.com, http://www.baidu2.com, http://www.baidu3.com, ]
a. 迴圈列表,為每一個URL產生Socket對象
b. 每一個socket對象,向遠程發送連結請求 connect: 阻塞
c. 如果串連: 發送資料:遵循格式
d. 擷取響應內容
e. 關閉 注意:可讀和可寫狀態 參考部落格:http://www.cnblogs.com/wupeiqi/articles/6229292.html
產出:
1. setblockint(False)
2. select監聽其他對象: def fileno():...
3. gevent,twisted,asycio ---> 單線程並發發送Http請求
2. Paramiko 參考部落格:http://www.cnblogs.com/wupeiqi/articles/5095821.html - 是一個模組,socket,SSH - Python代碼,實現遠程伺服器操作
功能:
a. 使用使用者名稱密碼: - 命令 SSHClient -> Transport - 檔案 Transport
b. 使用使用者名稱秘鑰: - 命令 SSHClient -> Transport - 路徑,也支援字串形式 - 檔案 Transport ====> SSH協助類 <====
c. 執行建立session # Bastion Host Bug
產出:
- 連結遠程,操作:命令,檔案
3. MySQL - 什麼是MySQL 服務端:
a.socket服務端運行,監聽:IP和連接埠
b.擷取用戶端發送的資料: asdfasdfasdfasdfasdfasdf
c.解析
d.去檔案中做操作
用戶端:
a.socket用戶端:基於各種語言的用戶端
b.驗證 c.發送命令(學習規則SQL語句) - 安裝
服務端:
用戶端: - 基本使用:
a. 申請 使用者: 授權: 使用者名稱,資料庫(檔案夾),表(檔案),增刪改查,IP
b. 串連 mysql -u root -h c1.com -p asdfasdf
mysql -u wupeiqi -h 192.168.12.111 -p 123
c. 規則
檔案夾(資料庫):
create database db1 default charset utf8; drop database db1; show databases; use db1;
檔案(表):
show tables; create table tb1( id int not null auto_increment primary key, name char(20) null default 1, age int not null)engine=innodb default charset utf8;
# 增
insert into tb1(name,age) values(‘alex‘,18);
# 刪
delete from tb1 where id>1;
# 改
update tb1 set age=168 where id=2;
#查
select * from tb1;
外鍵:foreign key 一對多
create table deparment( id int not null auto_increment primary key, title char(32) null ) create table person( id int not null auto_increment primary key, username char(32) null , age int not null, deparment_id int not null, constraint fk_cc foreign key (deparment_id) references deparment(id) )
雙向的外鍵:
多對多 create table deparment( id int not null auto_increment primary key, title char(32) null ) create table host( id int not null auto_increment primary key, ip char(32) null, port char(32) null ) create table de_2_host( id int not null auto_increment primary key, did int not null, hid int not null, constraint fk_did_deparment foreign key (did) references deparment(id), constraint fk_hid_host foreign key (hid) references host(id) )
連表:
select * from person; select * from person left join deparment on person.deparment_id = deparment.id
# 1 alex 18 1 1 諮詢
2 oldboy 68 3 3基礎
select id from deparment where title = "基礎" select hostid from host_deparment where deparment_id=1111 select * from host id in (1,2,3) #######
4. pymysql pip3 install pymysql # 內部socket import pymysql # 建立串連 conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, passwd=‘123‘, db=‘t1‘)
# 建立遊標
cursor = conn.cursor() username = input(‘請輸入使用者名稱:‘) pwd = input(‘請輸入密碼:‘)
# 執行SQL,並返回受影響行數
#effect_row = cursor.execute("select * from userinfo where username=%s and pwd = %s", (username,pwd,)) # root or 1==1 --
# adfasdf # sql = "select * from userinfo where username=%s and pwd = %s" %(username,pwd,) # select * from userinfo where username=root or 1==1 -- and pwd = %s #effect_row = cursor.execute(sql)
# 提交,不然無法儲存建立或者修改的資料 conn.commit()
# 關閉遊標 cursor.close() # 關閉串連 conn.close()
python操作mysql資料庫