如何在文本文件中检查每个IP的index.php

时间:2015-01-06 21:00:57

标签: python httpwebrequest

我试图为我的列表txt中的每个ip创建一个脚本,并使用连接检查索引是否存在,如果存在,则使用相对检查路径向ip文件写入ip

我试过这个

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

1 个答案:

答案 0 :(得分:0)

我不熟悉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)