首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >文本文件编辑和行编写

文本文件编辑和行编写
EN

Stack Overflow用户
提问于 2018-04-18 04:30:31
回答 1查看 25关注 0票数 0

我的代码有一个小问题。

首先是代码:

代码语言:javascript
运行
复制
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中的某些特定行可以工作,但之后我不能在特定行上写入测量状态,

我得到了这个错误:

代码语言:javascript
运行
复制
Traceback (most recent call last):
  File "ping_All.py", line 48, in <module>
    data = (res[ip])
IndexError: string index out of range

希望有人能帮我一下。

EN

回答 1

Stack Overflow用户

发布于 2018-04-18 05:25:49

当您尝试访问该字符串中的项时,将得到index out of range。这意味着您试图访问一个索引(ip)大于res长度或小于0的项。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49886985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档