我的代码有一个小问题。
首先是代码:
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()问题:
读取.txt中的某些特定行可以工作,但之后我不能在特定行上写入测量状态,
我得到了这个错误:
Traceback (most recent call last):
File "ping_All.py", line 48, in <module>
data = (res[ip])
IndexError: string index out of range希望有人能帮我一下。
发布于 2018-04-18 05:25:49
当您尝试访问该字符串中的项时,将得到index out of range。这意味着您试图访问一个索引(ip)大于res长度或小于0的项。
https://stackoverflow.com/questions/49886985
复制相似问题