目的:
- python的基本输入输出函数和它的起源与安装
- python的判断语句与运算符号、循环语句
- Python变量属性
- Python文件操作
- Python函数与模块
- Python多线程编程
- 爬虫之web页面数据获取与正则
- 爬虫之图片获取与漫画下载
- 爬虫之web页面文字获取、豆瓣书籍、获取多本书
- Socket套接字编程、带菜单的脚本制作
- 后门制作和后门远程控制攻防
- Python脚本实战和信息收集扫描
- Python实战之Linux密码暴力破解
起止时间: 2022年12月12日 - 2023年1月 3日
地点:居家
python的判断语句与运算符号、循环语句
1.while的经典⽤法
案例1:猜拳-连续猜三次
import random
lista = ["石头","剪刀","布"]
winlist = ["石头剪刀","布石头","剪刀布"]
num = 0
while num <3:
num += 1
pc_choice = random.choice(lista)
ren_choice = input("请输入您的猜拳:")
if ren_choice+pc_choice in winlist:
print("您出的是%s,机器出的是%s,您赢了"%(ren_choice,pc_choice))
elif ren_choice == pc_choice:
print("您出的是%s,机器出的是%s,平局"%(ren_choice,pc_choice))
else:
print("您出的是%s,机器出的是%s,您输了"%(ren_choice,pc_choice))
案例2:10以内的整数和
sumnum = 0
num = 0
while num<10:
num+=1
sumnum += num
print(sumnum)
案例3:10以内的偶数和
sumnum = 0
num = 0
while num<10:
num+=1
if num % 2 == 0:
sumnum += num
print(sumnum)
案例4:10以内的奇数和
sumnum = 0
num = 0
while num<10:
num+=1
if num % 2 != 0:
sumnum += num
print(sumnum)
案例5:随机⽣成8位密码
import string
import random
allstring = string.ascii_letters +string.digits+string.punctuation
password = ""
num = 0
while num<8:
num+=1
password += random.choice(allstring)
print(password)
案例6:while嵌套循环
i = 0
while i < 3:
i+=1
print("hello",i)
j = 0
while j<5:
j+=1
print("hi",j)
2、break
案例1:
num = 0
while True:
num+=1
print("hello,zxh")
if num == 5:
break
案例2:猜数字游戏(猜对为⽌)
import random
pcnum = random.randint(1,10)
while 1:
rennum = int(input("请输入一个数字:"))
if pcnum>rennum:
print("小了")
elif pcnum<rennum:
print("大了")
else:
print("对了")
break
案例3:猜拳-三局两胜
import random
lista = ["石头","剪刀","布"]
wincount =0
lostcount = 0
winlist = ["石头剪刀","布石头","剪刀布"]
while True:
ren_choice = input("请输入您的猜拳:")
pc_choice = random.choice(lista)
if ren_choice+pc_choice in winlist:
print("您出的是%s,机器出的是%s,您赢了"%(ren_choice,pc_choice))
wincount+=1
elif ren_choice == pc_choice:
print("您出的是%s,机器出的是%s,平局"%(ren_choice,pc_choice))
else:
print("您出的是%s,机器出的是%s,您输了"%(ren_choice,pc_choice))
lostcount+=1
if wincount == 2:
print("赢了")
break
elif lostcount == 2:
print("输了")
break
案例4:嵌套中break的⽤法
i = 0
while i < 3:
i+=1
print("hello",i)
j = 0
while j<5:
j+=1
print("hi",j)
if j == 3:
break
if i == 2:
break
3、continue
案例1
sumnum = 0
num = 0
while num < 10:
num += 1
if num % 2 != 0:
continue
sumnum += num
print(sumnum)
案例2 猜拳游戏-三局两胜(⽤索引取值)
import random
lista = ["石头","剪刀","布"]
wincount =0
lostcount = 0
winlist = ["石头剪刀","布石头","剪刀布"]
while True:
ren_num = input("请输入您的猜拳:0-石头 1-剪刀 2-布:")
pc_choice = random.choice(lista)
if ren_num not in "012":
continue
ren_choice = lista[int(ren_num)]
if ren_choice+pc_choice in winlist:
print("您出的是%s,机器出的是%s,您赢了"%(ren_choice,pc_choice))
wincount+=1
elif ren_choice == pc_choice:
print("您出的是%s,机器出的是%s,平局"%(ren_choice,pc_choice))
else:
print("您出的是%s,机器出的是%s,您输了"%(ren_choice,pc_choice))
lostcount+=1
if wincount == 2:
print("赢了")
break
elif lostcount == 2:
print("输了")
Break
4、for
案例1:
for i in range(3): # 从0开始到2的整数区间,3不做输出
print(i)
for i in range(1,3): # 从1开始到2的整数区间,3不做输出
print(i)
for i in range(1,11): # 从1开始到10的整数区间,11不做输出
print(i)
for i in range(1,11,2): # 结果1,3,5,7,9,11不做输出,2代表步⻓
print(i)
案例3:列表取值
lista = ["dafeige","tom","jack"]
for i in lista:
print("hello",i)
爬虫之图片获取与漫画下载、web页面文字获取、豆瓣书籍、获取多本书
过程:
python伪装成浏览器,发送一个http请求头,得到服务器响应后,提取HTML源码并且打印输出
urllib是模块包,request是模块包urllib中的一个模块
import urllib.request
response = urllib.request.urlopen("http://www.autewifi.com")#具有了构造请求头的功能
html = response.read()#读取源码放入内存中
print(str(html,encoding="utf8"))#将二进制数据转换为字符串类型
使用python访问后:
被发现用python互动:
伪造http请求头:
python人工构造一个虚假的请求头:
代码:
import urllib.request as r
url = "http://192.168.246.137/"
request = r.Request(url)#用ruquest函数根据url构造了一个请求头request
request.add_header("User-Agent","Mozilla/5.0 (Windows NT 12.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.188.400 QQBrowser/11.4.5225.400")
response = r.urlopen(url)#具有了构造请求头的功能
html = response.read()#读取源码放入内存中
print(str(html,encoding="utf8"))#将二进制数据转换为字符串类型
查看日志:
代码:
import urllib.request as r
def get_html(url):
"给我一个url,返回该url的html源码"
request = r.Request(url)#用ruquest函数根据url构造了一个请求头request
request.add_header("User-Agent","Mozilla/5.0 (Windows NT 12.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.188.400 QQBrowser/11.4.5225.400")
response = r.urlopen(url)#具有了构造请求头的功能
html = response.read()#读取源码放入内存中
return html
if __name__ == "__main__":#调试
url = "http://192.168.246.137/"
html = get_html(url)
print(str(html,encoding="utf8"))#将二进制数据转换为字符串类型
爬取一张图片:
代码:
import urllib.request as r
def get_html(url):
"给我一个url,返回该url的html源码,给我一个图片网址,我给你图片源码"
request = r.Request(url)#用ruquest函数根据url构造了一个请求头request
request.add_header("User-Agent","Mozilla/5.0 (Windows NT 12.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.188.400 QQBrowser/11.4.5225.400")
response = r.urlopen(url)#具有了构造请求头的功能
html = response.read()#读取源码放入内存中
return html
if __name__ == "__main__":#调试
url = "http://192.168.246.137/style/u24020836931378817798fm170s6BA8218A7B2128178FA0A49F010080E2w.jpg"
html = get_html(url)
print(html)#将二进制数据转换为字符串类型
给一个图片源码,可以实现保存:
代码:
import urllib.request as r
def get_html(url):
"给我一个url,返回该url的html源码,给我一个图片网址,我给你图片源码"
request = r.Request(url)#用ruquest函数根据url构造了一个请求头request
request.add_header("User-Agent","Mozilla/5.0 (Windows NT 12.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.188.400 QQBrowser/11.4.5225.400")
response = r.urlopen(url)#具有了构造请求头的功能
html = response.read()#读取源码放入内存中
return html
def download_image(imagecode):
f = open("D:\\python课程\\pic\\1.jpg","wb")
f.write(imagecode)
f.close()
if __name__ == "__main__":#调试
url = "http://192.168.246.137/style/u24020836931378817798fm170s6BA8218A7B2128178FA0A49F010080E2w.jpg"
imagecode = get_html(url)
download_image(imagecode)
给一个多个图片的地址列表,可以逐一下载下来:
代码:
import urllib.request as r
def get_html(url):
"给我一个url,返回该url的html源码,给我一个图片网址,我给你图片源码"
request = r.Request(url)#用ruquest函数根据url构造了一个请求头request
request.add_header("User-Agent","Mozilla/5.0 (Windows NT 12.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.188.400 QQBrowser/11.4.5225.400")
response = r.urlopen(url)#具有了构造请求头的功能
html = response.read()#读取源码放入内存中
return html
def download_image(imagelist):#得到源码下载图片
num = 0
for i in imagelist:
imagecode = get_html(i)
num += 1
f = open("D:\\python课程\\pic\\%s.jpg"%(num),"wb")
f.write(imagecode)
f.close()
if __name__ == "__main__":#调试
urllist = ["http://192.168.246.137/style/u24020836931378817798fm170s6BA8218A7B2128178FA0A49F010080E2w.jpg",\
"http://192.168.246.137/style/u1257164168471355846fm170s9A36CD0036AA1F0D5E9CC09C0100E0E3w6.jpg"]
download_image(urllist)
过程总结:
首先拿到一个网址,调用get_html(url)函数得到该网页的源码
然后将拿到的源码交给第二个函数get_imagelist(html),第二个函数进行处理后返回所有图片的地址列表
最后将图片地址传给download_image(imagelist)函数,将图片下载
下来
代码:
import urllib.request as r
import re
def get_html(url):
"给我一个url,返回该url的html源码,给我一个图片网址,我给你图片源码"
request = r.Request(url)#用ruquest函数根据url构造了一个请求头request
request.add_header("User-Agent","Mozilla/5.0 (Windows NT 12.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.188.400 QQBrowser/11.4.5225.400")
response = r.urlopen(url)#具有了构造请求头的功能
html = response.read()#读取源码放入内存中
return html
def get_imagelist(html):#给我一个网站的源码,给你获取所有图片的源码,返回一个列表
#imagelist = re.findall("style/.*?.jpg",str(html,encoding = "utf8"))
#imagelist = re.findall("style/\w*?.jpg",str(html,encoding = "utf8"))
#左邻字符只能是0~9 a~z A~Z
imagelist = re.findall("style/\w{60}.jpg",str(html,encoding = "utf8"))
imagelist2 = []
for i in imagelist:
i = url + i
imagelist2.append(i)
#print(imagelist2)
return imagelist2
def download_image(imagelist):#得到源码下载图片
num = 0
for i in imagelist:
imagecode = get_html(i)
num += 1
f = open("D:\\python课程\\pic\\%s.jpg"%(num),"wb")
f.write(imagecode)
f.close()
if __name__ == "__main__":#调试
url = "http://192.168.246.137/"
html = get_html(url)
imagelist = get_imagelist(html)
download_image(imagelist)
爬取漫画:
代码:
import urllib.request as r
import re
def get_html(url):
"给我一个url,返回该url的html源码,给我一个图片网址,我给你图片源码"
request = r.Request(url)#用ruquest函数根据url构造了一个请求头request
request.add_header("User-Agent","Mozilla/5.0 (Windows NT 12.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.188.400 QQBrowser/11.4.5225.400")
response = r.urlopen(url)#具有了构造请求头的功能
html = response.read()#读取源码放入内存中
return html
def get_imagelist(html):#给我一个网站的源码,给你获取所有图片的源码,返回一个列表
imagelist = re.findall("https://images.manmanapp.com/comic/small/\w{13}.jpg-cs1080",str(html,encoding = "utf8"))
return imagelist
def download_image(imagelist):#得到源码下载图片
num = 0
for i in imagelist:
imagecode = get_html(i)
num += 1
f = open("D:\\python课程\\漫画\\%3d.jpg"%(num),"wb")
f.write(imagecode)
f.close()
if __name__ == "__main__":#调试
url = "https://www.manmanapp.com/comic/detail-1562096.html"
html = get_html(url)
imagelist = get_imagelist(html)
download_image(imagelist)
python文件对象的基本操作
案例1:读取文件-read()
file = open("D:\\python课程\\111.txt","rb")
content = file.read()
#从光标一直读到尾部,赋值给content
print(content)
#打印content
file.seek(0,0)
content1 = file.read(5)
print(content1)
#从光标往后读取5个字符,赋值给content2
file.seek(3,0)
content2 = file.read()
print(content2)
#将光标置零,再往后推3个字符开始读
file.close()
案例2:读取文件-readlines()
file = open("D:\\python课程\\111.txt","rb")
#逐行读取
content = file.readlines()
print(content)
file.close()
案例3:读取文件-编码读取
file = open("D:\\python课程\\111.txt","r",encoding = "utf8")
#将编码方式改为与win10对应的utf8
content = file.readlines()
print(content)
file.close()
案例4:写入文件
file = open("D:\\python课程\\111.txt","w")
file.write(b"hello world")#数字类型需要为byte
file.close()
案例5:写入文件-writelines()
默认覆盖写
file = open("D:\\python课程\\111.txt","w")
lista = ["hello \n","zxh\n","666\n"]
file.writelines(lista)
file.close()
案例6:实现追加写入文件内容
案例7:实现复制文件
练习:实现复制cmd.exe
file = open("C:\\Windows\\System32\\cmd.exe","rb")
content = file.read()
file.close()
file2 = open("C:\\Users\\666666\\Desktop\\666.exe","wb")
file2.write(content)
file2.close()
Python 文件对象进阶操作
案例1:通过特殊方式打开文件
with open("D:\\python课程\\111.txt","rb") as file:
content = file.read()
print(content)
print(file.closed)
案例2:按指定文件大小分段复制cmd.exe
#开启读写操作
file1 = open("C:\\Windows\\System32\\cmd.exe","rb")
file2 = open("C:\\Users\\666666\\Desktop\\777.exe","wb")
#进行读写操作
while True:
data = file1.read(4096)
if not data == b"":
file2.write(data)
else:
break
#关闭读写操作
file1.close()
file2.close()
综合案例:抽签软件
import random
f1 = open("D:\\python课程\\学生列表.txt","r",encoding = "utf8")
lista = f1.readlines()
f1.close()
userlist = []
for i in lista:
userlist.append(i.strip())#将空白字符去掉
luckyuser_list = []
while True:
num = input("请输入一个数字完成抽签:1=抽签 2=退出:")
if num == "1":
lucky_user = random.choice(userlist)
print("恭喜,本次抽签抽中了:",lucky_user)
luckyuser_list.append(lucky_user)
userlist.remove(lucky_user)
if userlist == []:
print("没有可以抽签的用户了,抽签结束")
print("抽签结束,本次抽签的幸运用户列表为:",luckyuser_list)
break
elif num == "2":
print("抽签结束,本次抽签的幸运用户列表为:",luckyuser_list)
break
else:
print("输错了,只能输入1或者2")
continue
编写简易的聊天程序
①创建服务器脚本程序
②编写服务器程序
对方连我,我接受请求,形成一个回话
我给对方打招呼,对方给我回一个招呼,会话结束
③在win11上编写客户机程序
④运行服务器程序,监听状态
⑤运行客户机程序
连接完成
3聊天不中断程序
①kali服务器端
②win10客户端
代码:
import socket
zxhclient = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ipport = ("192.168.246.137",5000)
zxhclient.connect(ipport)
data = zxhclient.recv(1024)
print(str(data,encoding = "utf8"))
while True:
data = input("请输入你要说的话:")
zxhclient.send(bytes(data,encoding="utf8"))
if data == "byebye":
break
data2 = zxhclient.recv(1024)
print(str(data2,encoding="utf8"))
if data2 == b"byebye":
break
zxhclient.send(b"good\n")
zxhclient.close()
③运行
④bug:当输入为空,双方都进入等待状态,死循环
对话:
如何做有菜单的脚本
代码:
import string
import random
import re
def guess_quan():
print("我是一个猜拳游戏")
def guess_password():
print("我是一个可以生成随机密码的函数")
def guess_num():
print("我是一个猜数字的游戏")
def chat_client():
print("我是一个可以连接服务器进行简易聊天的程序")
def menu():
while True:
num = input("请输入你的选择:1-猜拳游戏 2-生成随机密码 3-猜数字 4-简易聊天程序 5-退出程序")
if num == "1":
guess_quan()
elif num == "2":
guess_password()
elif num == "3":
guess_num()
elif num == "4":
chat_client()
elif num == "5":
break
else:
continue
print("byebye")
if __name__ == "__main__":
menu()