嗨,伙计们,我是python新手,我试着完成一项任务。 任务如下:我需要读取一个名为“的文件”ip.txt文件“然后逐行读取文件,并使用urlopen将txt文件行传递给URL。你知道吗
这是我的剧本
from urllib.request import urlopen
def send_alert():
from smtplib import SMTP
from email.mime.text import MIMEText
msg = MIMEText('nuf said')
msg['Subject'] = 'inventi.lt is unreachable'
msg['From'] = 'XXX@xxxx.com'
msg['To'] = 'jnin@xxxx.com'
server = SMTP('mail.xxxxxx.com:2525')
server.ehlo()
server.starttls()
server.login('jnin@xxxxx.com', 'password')
server.sendmail('jnin@xxxx.com', ['jnin@xxx.com'], msg.as_string())
server.quit()
###### LEER FILE
ip_dirreciones = open("ip.txt", "r")
try:
urlopen ('http://VARIABLE-HERE/cgi-bin/api-sys_operation?passcode=2000&request=REBOOT')
print ("OK")
except:
send_alert()
其思想是脚本将自动运行文本文件中的每一行
样品
http://1.1.1.1/cgi-bin/api-sys_operation?passcode=2000&request=REBOOT
http://1.1.1.3/cgi-bin/api-sys_operation?passcode=2000&request=REBOOT
http://1.1.1.2/cgi-bin/api-sys_operation?passcode=2000&request=REBOOT
更新
我取得了一些进步,但不是一路工作。我只需要脚本能够阅读和使用ip.txt文件文件。你知道吗
它只读第一行然后停下来。 这是密码
from urllib.request import urlopen
def send_alert():
from smtplib import SMTP
from email.mime.text import MIMEText
msg = MIMEText('nuf said')
msg['Subject'] = 'inventi.lt is unreachable'
msg['From'] = 'email@email.com'
msg['To'] = 'email@email.com'
server = SMTP('mail.domain.com:2525')
server.ehlo()
server.starttls()
server.login('email@email.com', 'password!')
server.sendmail('email@email.com', ['email@email.com'], msg.as_string())
server.quit()
###### LEER FILE
f = open("ip.txt", "r")
for x in f:
# print('http://'+ x +'/cgi-bin/api-sys_operation?passcode=2000&request=REBOOT')
try:
urlopen ('http://'+ x +'/cgi-bin/api-sys_operation?passcode=2000&request=REBOOT')
print ("OK")
except:
send_alert()
你从这样的文件中读到:
这将创建一个列表,其中
.txt
文件的每一行都将转换为列表中的一个项。然后您可以遍历此列表依次使用每个项!你知道吗如果您的问题是关于组合字符串以生成链接(如.txt文件中包含:
然后你会说:
这将动态地用迭代中的
link
填充{}
@编辑
在你的评论中,你说你的头在
b'192.168.0.77\n'
有一个错误,这是因为你给你的程序一堆字节,而它需要一个字符串。这可以通过以下方法避免:唯一的区别是我们把它转换成一个字符串!你知道吗
相关问题 更多 >
编程相关推荐