1

我正在尝试创建一个 python 脚本来检查主机是否处于活动状态,如果是,则将网站下载到 results/ 目录中。一旦我学会了如何做到这一点,我将开始研究如何爬取和启动其他子进程(例如在检查完成后启动 nikto/skipfish 并加载保存的文件)。

#! /usr/bin/python

import os
import sys
import urllib
import urllib2
import subprocess

# Where the magic happens

str1 = raw_input("Enter your target: ")
print "Target = ", str1
print "commencing testing on", str1

# Let's set the user-agent headers
http_headers = {"User-Agent":"Mozilla/5.0"}

request = urllib2.Request(str1)
response = urllib2.urlopen(request)
payload = response.read()

dir_path = os.path.join(self.results)
os.makedirs(dir_path)
**with open(os.join.path(dir_path, 'index.html', 'wb') as file:
        file.write(payload)
print str1, "index written to file"**

# Send an email to notify us when complete
var = "world"
pipe = subprocess.Popen(["./email.sh", var], stdout=subprocess.PIPE)
result = pipe.stdout.read()
print result

我收到以下错误消息:

File "./webtest.py", line 43
    with open(os.join.path(dir_path, 'index.html', 'wb') as file:
                                                          ^
SyntaxError: invalid syntax

关闭括号后的错误(来自菲尔的回答):

    Traceback (most recent call last):
      File "./webtest.py", line 41, in <module>
        dir_path = os.path.join(self.results)
NameError: name 'self' is not defined
4

1 回答 1

1

你错过了一个括号:

with open(os.join.path(dir_path, 'index.html', 'wb')) as file:

编辑

该行与您想要的目录有关。它给出错误是因为你不在课堂上(所以“自我”不存在)。最好的做法是用“结果”替换它并指定结果在哪里。例如:

results = "/resultsdir/"
dir_path = os.path.join(results)
于 2013-01-29T18:11:48.283 回答