我正在尝试设置一个python脚本来测试服务器是否启动,如果不是,请给我发电子邮件。我目前正在尝试实现该目标的电子邮件部分。
我的代码如下:
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.action_chains import ActionChains
from urllib.request import urlopen
from html.parser import HTMLParser
import smtplib
binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path='C:\geckodriver-v0.18.0-win64\geckodriver.exe')
class PythonOrgSearch(unittest.TestCase):
#sets up driver to run tests
def setUp(self):
self.driver = driver
def testServer(self):
driver = self.driver
server = smtplib.SMTP('smtp.gmail.com', 587)
#Next, log in to the server
server.login("email@gmail.com", "password")
#Send the mail
msg = "Testing if server is down" # The /n separates the message from the headers
server.sendmail("email@google.com", "email@google.com", msg)
driver.close()
if __name__ == "__main__":
unittest.main()
当我运行它时,我得到的错误是driver.close()是不正确的语法。 但是,它在我拥有的每个其他测试脚本上都能正常工作。 在弹出此错误之前,脚本将运行并且永远不会关闭,并且永远不会发送电子邮件。我不确定我错过了什么。如果您发现问题,请告诉我。谢谢!
答案 0 :(得分:1)
你有开括号:
server.sendmail(("email@google.com", "email@google.com", msg)
更改为:
server.sendmail("email@google.com", "email@google.com", msg)