[ichunqiu笔记] python安全应用编程入门

01 python正则表达式

02 Python Web编程

03 Python多线程编程

04 Python网络编程

05 Python数据库编程

-------------------------------------

01 python正则表达式

对字符串的匹配和检索,通过re模块提供对正则表达式的支持。

 

. 匹配任意换行符以外的字符

1 #python 3.6
2 import re
3 word = 'https://www.ichunqiu.com python_1.1'
4 key = re.findall('h.',word)
5 print (key)
结果是['ht', 'hu', 'ho']

\ 转义字符

#python 3.6
import re
word = 'https://www.ichunqiu.com python_1.1'
key = re.findall('\.',word)
print (key)
['.', '.', '.']

 [...]字符集。对应的位置可以是字符集中任意字符,可以逐个列出也可以给出范围,如[abc]或[a-c]。第一个字符是^则代表取反,[^abc]代表不是abc的其他字符

 预定义字符集

\d   数字[0-9]   a\dc  a1c

\D  非数字[^\d]   a\Dc  abc

\s   空白字符   a\sc   a c

\S  非空白字符       abc 

\w  单词字符[a-z A-Z 0-9] abc

\W  非单词字符      a c

*   匹配前一个字符0次或无限次

+  匹配前一个字符1次或无限次

?  匹配前一个字符0次或1次

{m} 匹配前一个字符m次

{m,n} 匹配前一个字符m-n次

|   左右表达式任意匹配一个

(..) 一个分组

 贪婪模式与非贪婪模式

Python里的数量词默认是贪婪的,总是尝试匹配尽可能多的字符;非贪婪的则相反,总是尝试匹配尽可能少的字符。

 例如正则表达式ab*如果用于查找abbbc将找到abbbb,如果是非贪婪的ab*?,将找到a。

查找课程
http = ...
title = re.findall(r'title="(.*?)" onclick',http) for i in title: print (i)

02 python web编程

关键词:urllib/urllib/requests、爬虫开发

1 import urllib,urllib2
2 url = 'http://www.baidu.com'
3 r = urllib.urlopen(url) //发送请求
4 print r.read() //接受回显

 urllib.urlretreve(url,fliename = None,reporthook=None,data=None) //下载文件

urllib2.Requests() //控制请求头

urllib.urlretrieve('地址“',filename='E:\\google.png') //下载图片

requests

发送网络请求 requests.get(url) requests.post(url)  requests.head(url)

为URL传递参数payload={'key1':'value1';'key2','valu2'}  r = requests.get(url,params=payload)  //r.url=xxxx/get?key2=value2

响应内容 r.text r.content

定制请求头 headers = {'content-type':'application/json'}

复杂的POST请求payload={'key1':'value1';'key2','valu2'}  r = requests.post(url,data=payload)  

状态码 r.status_code

响应头 r.headers

Cookie r.cookies

请求超时 r = requests.get(url,timeout = 0.1)

网络爬虫

页码由字符串处理,加入headers头,可以用BP抓出来

r=request.get(url=url,headers=headers)

 

 03 python多线程

进程是程序的一次执行,每个进程都有自己的地址空间、内存、数据栈及其他记录其运行轨迹的辅助数据。

所有的线程运行在同一个进程当中,共享相同的运行环境。线程有开始顺序执行和结束三个部分。

start_new_thread(function,args kwargs=None)

 

 1 import thread
 2 import time
 3 def fun1():
 4     print 'Hello world!%s'%time.ctime()
 5 def main():
 6     thread.start_new_thread(fun1,())
 7     thread.start_new_thread(fun1,())
 8     time.sleep(2)
 9 if __name__ == '__main__':
10     main()
//一个简单的例子
 1 //探测C段存活主机
 2 #coding = utf-8
 3 #ping
 4 import thread
 5 import time
 6 from subprocess import Popen,PIPE
 7 
 8 def ping_check(ip):
 9     check = Popen(['ping.exe',ip],stdin=PIPE,stdout=PIPE)
10     data = check.stdout.read()
11     if 'TTL' in data:
12         print '[OK] %s'%ip
13 def main():
14     for i in range(1,255):
15         ip = '111.47.226.'+str(i)
16         thread.start_new_thread(ping_check,(ip,))
17         time.sleep(0.1)
18 if __name__ == '__main__':
19     main()

 

 

 threading模块

 1 import threading
 2 import time
 3 
 4 def fun1(key):
 5     print 'hello %s:%s'%(key,time.ctime())
 6 def main():
 7     threads=[]
 8     keys=['danny','nico','nick','pipe']
 9     threads_count = len(keys) //定义线程数
10     for i in range(threads_count):
11         t = threading.Thread(target = fun1,args=(keys[i],))
12         threads.append(t)
13     for i in range(threads_count):
14         threads[i].start()
15     for i in range(threads_count):
16         threads[i].join()
17 if __name__ == '__main__':
18     main()

 生产者-消费者问题和Queue模块

 Queue模块 qsize() empty()  full() put() get()

04 Python网络编程

 05 Python数据库编程

 

posted @ 2019-03-06 23:21  歇马  阅读(704)  评论(0编辑  收藏  举报