python安全渗透笔记

Python学习

python常见错误

UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x8c in position 22: illegal multibyte sequence //打开的文件未编码

encoding='UTF-8'

expected an indented block

未缩进,if for

continue 又进入一次新的循环

% 将其他变量置入字符串特定位置以生成新字符串的操作,但原来那个变量并不会改变

python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报错UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128),python没办法处理非ascii编码的,此时需要自己设置python的默认编码,一般设置为utf8的编码格式。

在程序中加入以下代码:即可将编码设置为utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

python3.x下应该改为如下方式:

import importlib

importlib.reload(sys)

报错

No module named 'pandas'  没有模块
TypeError: read() got an unexpected keyword argument ‘encoding‘  需要将x encoding='UTF-8'删除

当我们输入密码时

getpass模块无效,则使用input,让一个变量等于input
class Person(object):
    def __init__(self,name,score):#相当于是构造函数
        self.name = name     #name就是一个变量,我们需要进行赋值
        self.score = score
        
student = Person()      # 此处应该有参数传入,却没有传
print(student.name)
print(student.score)

在类中函数相互调用要加 self

多线程的理解

就好比有两个函数,要实现不同功能,正常情况下,我们只能先执行一个函数,然后再执行另外的函数,而在多线程的情况下,两个函数可以并发执行,当一个函数的线程结束后,线程数会自动减一,而如果将线程方法用for循环进行,则一个函数会有多个线程进行执行,与我们想要的结果也就不一样

"""
# 拆包enumerate返回值是一个元组
names = ["aa", "bb", "cc"]
for i, name in enumerate(names):
    print(i, name)
"""
import threading
import time


def test1():
    t=0
    for i in range(10):
        print("-----test1-----%s\n" % i)
        time.sleep(1)
        t+=1
        print('总的次数是%d'%t)
    # 如果创建Thread时执行的函数,函数运行结束意味着 这个子线程结束...


def test2():
    for i in range(5):
        print("-----test2-----%s\n" % i)
        time.sleep(1)


def main():
# for g in range(5):
    # 在调用thread之前打印当前线程信息
    print(threading.enumerate())
    # 创建线程
    t1 = threading.Thread(target=test1)
    t2 = threading.Thread(target=test2)

    t1.start()
    t2.start()

    # 查看线程数量
    while True:
        thread_num = len(threading.enumerate())
        print("线程数量是%d" % thread_num)
        if thread_num <= 1:
            break
        time.sleep(1)


if __name__ == '__main__':
    main()
    
    
    
###########################################################################################################

"""
# 拆包enumerate返回值是一个元组
names = ["aa", "bb", "cc"]
for i, name in enumerate(names):
    print(i, name)
"""
import threading
import time


def test1():
    t=0
    for i in range(10):
        print("-----test1-----%s\n" % i)
        time.sleep(1)
        t+=1
        print('总的次数是%d'%t)
    # 如果创建Thread时执行的函数,函数运行结束意味着 这个子线程结束...


def test2():
    for i in range(5):
        print("-----test2-----%s\n" % i)
        time.sleep(1)


def main():
    for g in range(5):
        # 在调用thread之前打印当前线程信息
        print(threading.enumerate())
        # 创建线程
        t1 = threading.Thread(target=test1)
        t2 = threading.Thread(target=test2)

        t1.start()
        t2.start()

    # 查看线程数量
    while True:
        thread_num = len(threading.enumerate())
        print("线程数量是%d" % thread_num)
        if thread_num <= 1:
            break
        time.sleep(1)


if __name__ == '__main__':
    main()



有for循环的也就会出现这样一个情况

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-trQrM2SW-1639054198305)(C:\Users\acer\AppData\Roaming\Typora\typora-user-images\image-20210616165232043.png)]


函数多线程

如果不进行线程的开始,函数就不会被调用,因为该脚本中没有进行调用函数,而这个脚本又与上面有所不同,虽说也有进行循环来开启多线程,但是由于有names进行判断,在进行多线程执行时,names值会减一,这里减一是全局减一,而不是说每一个线程对同一个值减一,实际上就是说一个数减一后,另外一个线程再拿减一的数进行减一
import  time
import threading
import math

def showName(threadNum,name):
    nowTime = time.strftime('%H:%M:%S', time.localtime(time.time())) # 获取当前时间
    print('I am thread-%d ,My name is function-%s, now time: %s ' % (threadNum, name, nowTime))
    time.sleep(1)
if __name__=='__main__':
    print('I am main...')
    names=list(range(20))
    threadNum=1
    threadPool=list()
    while names:
        for i in range(6):
            try:
                name=names.pop()
            except IndexError as e:
                print('The list is empty')
                break
            # else:
            #     t=threading.Thread(target=showName,args=(i, name))
            #     threadPool.append(t)
            #     t.start()
        while threadPool:
            t=threadPool.pop()
            t.join()
        threadNum+=1
        print('----------------------\r\n')
    print('main is over')

‘utf-8’) as fp

线程同步能够保证多个线程安全访问竞争资源,最简单的同步机制是引入互斥锁。互斥锁为资源引入一个状态:锁定/非锁定。某个线程要更改共享数据时,先将其锁定,此时资源的状态为“锁定”,其他线程不能更改;直到该线程释放资源,将资源的状态变成“非锁定”,其他的线程才能再次锁定该资源。互斥锁保证了每次只有一个线程进行写入操作,从而保证了多线程情况下数据的正确性。

cannot join thread before it is started  出现这个错误说明少了 start()方法

无Python环境解决

Pyinstaller可将Python代码生成EXE文件运行,解决内网无Python环境,现在电脑 环境无需再pyinstallser目录下

famous_person = "Steve Jobs once said"
message = "You've got to find what you love"
yiqi = famous_person + ',''"'+message+'"'
print(yiqi)

在终端输入参数,运行脚本

sys.argv[]说白了就是一个从程序外部获取参数的桥梁
比如有一个函数需要传参
def search(self,data,fofa)
简单的写法
data=sys.argv[1]
fofa=sys.argv[2]
search(data,fofa)

python add_argument() 的使用方法

import argparse
  
parser = argparse.ArgumentParser()
parser.add_argument('--sparse', action='store_true', default=False, help='GAT with sparse version or not.')
parser.add_argument('--seed', type=int, default=72, help='Random seed.')
parser.add_argument('--epochs', type=int, default=10000, help='Number of epochs to train.')
  
args = parser.parse_args()
  
print(args.sparse)
print(args.seed)
print(args.epochs)


输出
/home/user/anaconda3/bin/python3.6 /home/user/lly/pyGAT-master/test.py
False
72
10000
  
Process finished with exit code 0

每个参数解释如下:

name or flags - 选项字符串的名字或者列表,例如 foo 或者 -f, --foo。
action - 命令行遇到参数时的动作,默认值是 store。
store_const,表示赋值为const;
append,将遇到的值存储成列表,也就是如果参数重复则会保存多个值;
append_const,将参数规范中定义的一个值保存到一个列表;
count,存储遇到的次数;此外,也可以继承 argparse.Action 自定义参数解析;
nargs - 应该读取的命令行参数个数,可以是具体的数字,或者是?号,当不指定值时对于 Positional argument 使用 default,对于 Optional argument 使用 const;或者是 * 号,表示 0 或多个参数;或者是 + 号表示 1 或多个参数。
const - action 和 nargs 所需要的常量值。
default - 不指定参数时的默认值。
type - 命令行参数应该被转换成的类型。
choices - 参数可允许的值的一个容器。
required - 可选参数是否可以省略 (仅针对可选参数)。
help - 参数的帮助信息,当指定为 argparse.SUPPRESS 时表示不显示该参数的帮助信息.
metavar - 在 usage 说明中的参数名称,对于必选参数默认就是参数名称,对于可选参数默认是全大写的参数名称.
dest - 解析后的参数名称,默认情况下,对于可选参数选取最长的名称,中划线转换为下划线.
然后对应程序中的内容:action - 命令行遇到参数时的动作,默认值是 store。所以sparse返回的是 Ture,

platform模块

platform.system() 获取操作系统类型,windows、linux等

platform.platform() 获取操作系统,Darwin-9.8.0-i386-32bit

platform.version() 获取系统版本信息

os.system(‘clear’)

system函数可以将字符串转化成命令在服务器上运行;其原理是每一条system函数执行时,其会创建一个子进程在系统上执行命令行,子进程的执行结果无法影响主进程;

readlines() 方法用于读取所有行

python -m SimpleHTTPServer 端口号 设置web服务器

raw_input函数

raw_input([prompt]) 函数从标准输入读取一个行,并返回一个字符串(去掉结尾的换行符):

#!/usr/bin/python
# -*- coding: UTF-8 -*- 
 
str = raw_input("请输入:")
print "你输入的内容是: ", str

input函数

input([prompt]) 函数和 raw_input([prompt]) 函数基本类似,但是 input 可以接收一个Python表达式作为输入,并将运算结果返回。

#!/usr/bin/python
# -*- coding: UTF-8 -*- 
 
str = input("请输入:")
print "你输入的内容是: ", str

网页请求

请求网址并获取返回的状态码

request.get(url+payload_linux).status_code  #url+payload_linux为自己定义的

解决登录才能提取数据的问题

import requests #载入模块
headers={
	'cookie':'cookie值也可以使用;分隔开继续请求;result_per_page=20',
}
请求时直接载入 requests.get(urls,headers=headers).content

请求网页报错解决

通过try来规避错误,当然在输出内容时也要进行编码

try:
    data=requests.get(url=url,headers=headers)
    print(data.content.decode('utf-8'))
except Exception as e:
    time.sleep(0.5)
    pass

请求的页面信息出现乱码无法写入到文件中,如上面的data信息,在写入文件时先帮他编码

data=requests.get(url=url,headers=headers).content.decode('utf-8')
code=requests.get(urls,headers=headers,proxies=proxy).status_code  #接收状态码,proxies设置代理

请求时都需要将payload进行编码

payload = base64.b64encode(payload.encode('utf-8'))#payload为要请求的请求头

请求url时一般添加如下:

rep1=requests.get(url,headers=headers,allow_redirects=False,timeout=1)
这个很重要allow_redirects=False,timeout=1

python执行顺序

https://www.cnblogs.com/cnXuYang/p/8336370.html

Python文件操作

在进行文件写入时,想要覆盖原文件内容可以用‘w’,不要的话用’+a’

    with open(r'xuexitong.txt', 'w', encoding='utf-8') as f:
        f.write(data)
        f.close()
将文件读取
 file_name = 'resource.txt'
 
 with open(file_name) as file_obj:
     lines = file_obj.readlines()
     print(lines)

解决加载文件多出换行符问题

字典中有换行符\n,我们需要屏蔽,比如变量为bian,那么屏蔽的语句为bian.replace(’/n’,’’)

csv.writer().writerow()保存的csv文件,打开时每行后都多一行空行

for paths in open('fs.txt',encoding='utf=8'):
    url=''
    paths=paths.replace('\n','')

############################################################################################################
with open(path, mode='r+') as f1:
    for line in f1.readlines():
        subdomin = line.strip('\n')
        print(subdomin)
 两者的区别,第二个将文件中的内容以列表存放,删除回车,这样的话subdomain的值只会保留最后一行,最终的paths都会

############################################################################################################
import re
data = []
with open(r'student.txt','r',encoding="utf-8") as f:
    for line in f.readlines():
        line = line.strip('\n')
        data.append(line)
print(data)

这样也可以
f = open('student.txt',encoding='utf-8')
data = [line.strip() for line in f.readlines()]
f.close()
print(data)

对文件读取并保存在列表中,将列表写入到文件中并添加换行符

import re
data = []
with open(r'student.txt','r',encoding="utf-8") as f:
    for line in f.readlines():
        line = line.strip('\n')
        data.append(line)
print(data)
tidata = re.compile(r'(138\d{8})[^\d]')
datas = tidata.findall(str(data))
print(datas)
for aa in datas:
    with open(r'haoma.txt','w+',encoding='utf-8') as f:
        f.write(aa+'\n')
f.close()

字典中有换行符\n,我们需要屏蔽,比如变量为bian,那么屏蔽的语句为bian.replace(’/n’,’’) 也可以用strip(’\n’)

#以下内容解释,当我们进行网页爬取时,经常提取的内容会出现以下的形式
['加油','来吧','hhh \n']这样以列表的形式存储,此时需要进行如下设置,
tiqus = '\n'.join(tiqu)
tiquss=tiqus.split()

############################################################################################################
data = ['jksdf \n lksfja','kjasfdkjlk askjfdkfkkkkkkkkkkk']
result1 = '\n'.join(data)
print(result1)
result2 = result1.split()
print(result2)

输出
jksdf 
 lksfja
kjasfdkjlk askjfdkfkkkkkkkkkkk
['jksdf', 'lksfja', 'kjasfdkjlk', 'askjfdkfkkkkkkkkkkk']

使用 lxml提取内容

提取爬取的网页内容参考:https://www.cnblogs.com/zhangxinqi/p/9210211.html

from lxml import etree
# import re
# regs=r"ab+"
# data="acabc"
# m=re.search(regs,data)
# print(m)
with open(r'xuexitong.txt','r',encoding='utf-8') as rede:
    xue=rede.read()
soup=etree.HTML(xue)
# print(type(soup))
# html=etree.parse('soup',etree.HTMLParser())
tiqu=soup.xpath('//ul[@class="nav fr"]/li[@class="appCode"]/text()')
#寻找ul中属性值为class="nav fr"的ul标签,并且在该标签中存在子标签li,属性值为class="appCode"的内容

#以下内容解释,当我们进行网页爬取时,经常提取的内容会出现以下的形式
['加油','来吧','hhh \n']这样以列表的形式存储,此时需要进行如下设置,
# tiqus = '\n'.join(tiqu)
# tiquss=tiqus.split()
# print(tiquss)
for cichi in tiqu:
    with open(r'xueru.txt','a+',encoding='utf-8') as wc:
        wc.write(cichi+'\n')
        wc.close()
        
from lxml import etree

text='''
<div>
    <ul>
         <li class="item-0"><a href="link1.html">第一个</a></li>
         <li class="item-1"><a href="link2.html">second item</a></li>
     </ul>
 </div>
'''

html=etree.HTML(text,etree.HTMLParser())
result=html.xpath('//li[@class="item-1"]/a/text()') #获取a节点下的内容
result1=html.xpath('//li[@class="item-0"]//text()') #获取li下所有子孙节点的内容,并不包括second item,因为下面的并不属于子孙节点
result2=html.xpath('//div//text()')#li是他的子节点
result22='\n'.join(result2)#将列表使用\n分隔
result222=result22.split()#以空格为分隔符,将所有的\n和空格去除都去除
result3=html.xpath('//li/a/@href')  #获取a的href属性
result4=html.xpath('//li[@class="item-1"]//a/@href')#获取li属性为class="item-1"下的子节点a的href的属性值
result5=html.xpath('//li/a')#//li用于选中所有li节点,/a用于选中li节点的所有直接子节点a
print(result)
print(result1)
print(result22)
print(result3)
print(result4)
print(result5)

split的理解

processes = []

with open("file.txt", "r") as f:
    lines = f.readlines()
    #print(lines)
    # Loop through all lines, ignoring header.
    # Add last element to list (i.e. the process name)
    for l in lines[1:]:
       processes.append(l.split()[-1])#将获得的内容添加到processes列表中,空格为分隔符保留最后一个

print(processes)

正则表达式提取内容

https://blog.csdn.net/u013074465/article/details/44310427?locationnum=7&fps=1

https://blog.csdn.net/ysy_1_2/article/details/104790079

https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

https://www.runoob.com/python/python-reg-expressions.html

正则表达式在线测试网站:https://c.runoob.com/front-end/854/

正则表达式通关网站:https://codejiaonang.com/#/course/regex_chapter1/0/0

Python正则表达式表格:Python正则表达式指南 - AstralWind - 博客园 (cnblogs.com)

经验总结

1、提取文本中的内容一般不转换为列表

2、在进行正则匹配时,在要提取的数据前面有多个数据需要匹配时,一般不用正向断言,而是用(?😃

3、最后的提取的内容不需要数字时使用^,最后文本中的内容并没有换行,什么东西也没有

4、\s可以匹配空白字符,包括换行,如果内容未变成列表,换行使用\s匹配

5、在列表中换行变成了\n,要进行匹配的话要 两个\和一个n,而使用\s无法匹配

f[\d]?  代表f后面可以有数字也可以不要数字
f[\d]+  代表匹配数字一次或者无数次,表示f后面一定要有数字 比如faaa就不能提取
f[7]+    表示最少要有一个7,如果位f789的话就匹配f7,如果是f777就匹配f777
\d+    这样的并非是先匹配一个数字,而后一个数字就要一样,可以这样123,也可以这样11
{6,7}? 非贪婪模式,如果有七个则匹配六个
[abc]?  表示前面可以有abc,也可以没有
f[1]    f后面必须要有1
a.*?b   匹配最短的,以a开始,以b结束的字符串。如果把它应用于aabab的话,它会匹配aab和ab。
\bmaster\b  就仅匹配有边界的master单词
(138\d{8})[^1-9]   表示后面必须是非数字,并且有值,但问题来了,在文本中最后的内容没有值呀,但是它还是会匹配

使用正则表达式替换内容

inputStr = "hello 123 world 456"
replacedStr = re.sub("\d+", "222", inputStr)
输出
hello 222 world 222

#此正则表达式hello (\w+), nihao \1就是原inputStr的内容
inputStr = "hello crifan, nihao crifan";
weilai=re.search(r"hello (\w+), nihao \1",inputStr)
print(weilai)
replacedStr = re.sub(r"hello (\w+), nihao \1", "crifanli", inputStr);#分别为正则表达式,替换的内容,字符串
print("replacedStr=",replacedStr) #crifanli
re.compile

re模块中包含一个重要函数是compile(pattern [, flags]) ,该函数根据包含的正则表达式的字符串创建模式对象。可以实现更有效率的匹配。在直接使用字符串表示的正则表达式进行search,match和findall操作时,python会将字符串转换为正则表达式对象。而使用compile完成一次转换之后,在每次使用模式的时候就不用重复转换。当然,使用re.compile()函数进行转换后,re.search(pattern, string)的调用方式就转换为 pattern.search(string)的调用方式。

pattern = re.compile(r'44280335\s',re.S)
re.search函数
import re
result = re.search(r'nhooo', 'TP nhooo.com nhoo nhooTP')
print(result)#这样的话会返回对象
print(result.group(0))#可以返回值,但是这样就有一个鸡肋只能返回一个nhoo

去除文本中所有的标点

def removePunctuation(self, content):
        """
        文本去标点
        """
        punctuation = r"~!@#$%^&*()_+`{}|\[\]\:\";\-\\\='<>?,./,。、《》?;:‘“{【】}|、!@#¥%……&*()——+=-"
        content = re.sub(r'[{}]+'.format(punctuation), '', content)
        return content.strip().lower()

换行匹配

以下为正确的正则表达式匹配规则:
([\s\S]*)

同时,也可以用 “([\d\D])”、“([\w\W])” 来表示。 Web技术之家_www.waweb.cn

在文本文件里, 这个表达式可以匹配所有的英文
/[ -~]/

提取的内容为多行

将《unix网络编程(卷1)》源代码的使用方法提取出来

re.compile()函数的一个标志参数叫re.DOTALL,它可以让正则表达式中的点(.)匹配包括换行符在内的任意字符。

值得一看:https://www.cnblogs.com/dong973711/p/11923953.html#/c/subject/p/11923953.html

import re
neirong='''
< script
type = "text/ecmascript" >
window.quickReplyflag = true;
< / script >
< div
id = "article_details"
class ="details" >
< div
class ="article_title" >
< span
class ="ico ico_type_Original" > < / span >
< h1 >
<span class ="link_title"><a href="/u013074465/article/details/44280335">
《unix网络编程(卷1)》源代码的使用方法</a></span>
< / h1 >
< / div 
'''

pat='<span class ="link_title"><a href="/u013074465/article/details/44280335">\s(.*?)</a>'
rst1 = re.compile(pat, re.DOTALL).findall(neirong)
print(rst1)

提取的内容为一行

import re
data = []

with open(r'student.txt','r',encoding="utf-8") as f:
    datall=f.read()
'''    for line in f.readlines():
        line = line.strip('\n')
        data.append(line)
data1 = '\n'.join(data)
print(data1)
datall=str(data)
print(datall)
'''
print(datall)

#tidata = re.compile(r'(?<=[.年-])(\d{1,2}).(\d{1,2})')
tidata = re.compile(r'(?:.* \d{1,4}[.年-])(\d{1,2})[月.-](\d{1,2}).*')
datas = re.findall(tidata,datall)
#datas=re.split(tidata,str(datall))
print(datas)
for aa in datas:
    with open(r'haoma.txt','a',encoding='utf-8') as f:
        f.write(str(aa))
        f.close()

电话号码提取

import re
data = []
with open(r'student.txt','r',encoding="utf-8") as f:
    '''
    for line in f.readlines():
        line = line.strip('\n')
        data.append(line)
    '''
    data=f.read()
print(data)
tidata = re.compile(r'(138\d{8})[^\d]')
datas = tidata.findall(str(data))
print(datas)
for aa in datas:
    with open(r'haoma.txt','a',encoding='utf-8') as f:
        f.write(aa+'\n')
f.close()
findall函数

这个函数可以返回搜索到的所有值,返回数组

import re
neirong='''
< script
type = "text/ecmascript" >
window.quickReplyflag = true;
< / script >
< div
id = "article_details"
class ="details" >
< div
class ="article_title" >
< span
class ="ico ico_type_Original" > < / span >
< h1 >
<span class ="link_title"><a href="/u013074465/article/details/44280335">
《unix网络编程(卷1)》源代码的使用方法</a></span>
< / h1 >
< / div 
'''
pattern = re.compile(r'(?:<span class ="link_title"><a href="/u013074465/article/details/44280335">)\s(.*?)</a>',re.S) #re.S使得‘.’可以匹配任意字符
ret=pattern.findall(neirong)
#pat=r'(?:<span class ="link_title"><a href="/u013074465/article/details/44280335">\S)([\s\S]*?)(.*?)</a>'
#rst1 = re.search(pat,neirong)
#rst1 = re.compile(pat, re.DOTALL).findall(neirong)
print(ret)

1、有11位但是后面没有数字

2,以1开头

https://blog.csdn.net/weixin_44321116/article/details/104182394

在字符串a中,包含换行符\n,在这种情况下:

如果不使用re.S参数,则只在每一行内进行匹配,如果一行没有,就换下一行重新开始。

而使用re.S参数以后,正则表达式会将这个字符串作为一个整体,在整体中进行匹配。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VPOzjY0c-1639054198306)(C:\Users\acer\Desktop\信息安全\编程学习\python文档\pyhon学习自己做的笔记\python学习自己做的笔记.media\image-20211005232449498.png)]

删除文本中的(1)

定义列表出错AttributeError: ‘list’ object has no attribute ‘replace’

使用str函数

for D in range(1,100):                       #删掉句中()
    newlines = str(newlines).replace("({})".format(D),"") #其中newlines为列表,如果列表中有asd(1)ad这样的字符串后运行变成asdad,可以删除括号的内容,但是括号的内容不能超过10

Python处理Excel

安装xlrd读取Excel

安装xlwt写入库

安装xlutils库就能够进行修改

split()拆分字符串
'\n'.join 合并字符串,用\n合并
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值