如何对文本文件中的每个 IP 进行 index.php 检查

How to to do a index.php check for each IP in text file

我正在尝试为列表 txt 中的每个 ip 创建一个脚本,并检查索引是否存在,如果存在,则将 ip 与相对检查路径写入 txt 文件

我试过这个[=3=​​]

while line:
    print line
    import httplib
    conn = httplib.HTTPConnection( x )
    conn.request("HEAD", "/index.php")
    r1 = conn.getresponse()
    print r1.reason

import logging
if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO, filename="logfile", filemode="a+",
                    format="http://" + x + '/index.php' " " "%(message)s")
    logging.info( r1.reason )

    reload(logging)

ips.txt包含3个ip

8.8.8.8

9.9.9.9

7.7.7.7

我如何检查每个是否存在 index.php

example: 8.8.8.8/index.php 
If exist write 8.8.8.8/index.php  to result.txt
if not exist go to second ip

我不熟悉 httplib,但我想你需要捕获套接字错误:

import httplib
from socket import error

with open("in.txt") as f, open("out.txt","a") as out: # change to w if you want to overwrite each time
    for line in f:
        try:
            conn = httplib.HTTPConnection(line.rstrip())
            conn.request("HEAD", "/index.php")
            r1 = conn.getresponse()
            print(r1.reason)
            if r1.reason == "OK":
                out.write(line)
        except error as e:
            print(e)