[英]Text file editing and line writing
I have a slight problem with my code. 我的代码有一个小问题。
First the code: 首先是代码:
import os
import subprocess
import httplib, urllib
for ip in range(1,255):
ip_addr = "192.168.1." + str(ip)
res = subprocess.call(["ping", ip_addr, "-c1", "-W1", "-q"], stdout=open(os.devnull,'w'))
if res == 0:
print (ip_addr) + " reachable"
else:
print (ip_addr) + " not reachable"
f = open('state_All.txt','r')
prestate = f.readline(ip)
f.close()
prestate = ord(prestate)
res = res + 97
if not prestate == res:
if prestate == 97:
conn = httplib.HTTPSConnection("api.pushover.net:443")
## The Code in here which I have deleted out is definitely not the problem,
## works 100% safe and can't be share beacuse of security problems.
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
else:
conn = httplib.HTTPSConnection("api.pushover.net:443")
## The Code in here which I have deleted out is definitely not the problem,
## works 100% safe and can't be share beacuse of security problems.
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
f = open('state_All.txt','w')
res = str(unichr(res))
data = (res[ip])
f.writelines(data)
f.close()
Problem: 问题:
Reading some specific lines in the .txt works but I can't write on a specific line the measured state afterwards, 读取.txt文件中的某些特定行有效,但此后我无法在特定行中写入测量状态,
I got this error: 我收到此错误:
Traceback (most recent call last):
File "ping_All.py", line 48, in <module>
data = (res[ip])
IndexError: string index out of range
Hope someone can help me with it. 希望有人可以帮助我。
When you try access an item from that string, you get index out of range
. 当您尝试从该字符串访问项目时,
index out of range
。 This means that you tried to access an item, in your case a Character, where the index ( ip
) was either larger than the length of res
or smaller than 0. 这意味着您尝试访问索引(
ip
)大于res
长度或小于0的项目(以您的情况为Character)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.