4

所以我试图修改一些 HTML 以具有一个按钮,该按钮启动一个 python 脚本,该脚本作用于自己系统中的一些文件。

更具体地说,我有一个 python 脚本,它可以读取 snort 日志并生成一个指向新网站的链接(实际上,一个 IP 地址,它采用数据包捕获流搜索的参数)我正在尝试将此脚本实现为一个按钮一个网站。-注意:如果单击按钮的人需要在他们自己的机器上拥有 script.py,那没关系

试图在这个主题上做我的功课,但似乎有无限的选择——其中大部分都没有得到任何两方的同意。我需要一个框架吗?我可以不只是使用我的 HTML 代码中的一些 < script /> 或某些东西从其目录中调用我的 python 脚本吗?

4

3 回答 3

2

简短的回答:你不能。

您不能从浏览器访问用户机器上的文件,更不能执行它们。想象一下会有多大的安全漏洞。

但是,您可以在许多 GUI 工具包(Qt 或 wx 具有 Web 视图或类似工具)中实现自己的简单 Web 浏览器(例如,显示单个页面)。或者,您需要为您正在使用的浏览器开发(或查找)一个插件/插件,并与之通信。这将取决于每个浏览器等。我不知道这是否可行。或者,用户将下载一个他将选择运行而不是保存的文件,该文件将执行您的脚本。

实际上,我刚刚看到这个这个基本上是您安装的插件(基于 IronPython 和 Silverlight),但我不确定您是否可以执行用户系统上的脚本。它可以执行嵌入在页面中的代码。

于 2012-06-27T19:54:41.020 回答
1

OPs 问题的替代解决方案:

解决方案概要:

  1. 通过带有“GET”的 HTML 表单发送用户输入
  2. 处理来自发送到 shell 脚本的 url 编码值“GET”中的值
  3. Shell 脚本解析值并保存它们,将参数传递给 Python 脚本,同时调用它运行。

Javascript 和 php 可以很好地与此设置配合使用,并允许从那里使用 mysql 等。

使用“GET”,我们使用 shell 脚本将用户的输入从客户端发送到服务器端来处理我们的数据。

示例 Index.php

<!DOCTYPE html>
<html>
    <head>
    <title>Google Email Search</title>  
    </head>
<body>
    <h1>Script Options</h1>
<form action="/cgi-bin/call.sh" method="get">       
    <TABLE BORDER="1">
        <TR>
            <TD>Keyword:</TD>
            <TD><input type="text" name="query" value="Query"></TD>
        </TR>
        <TR>
            <TD># of Pages:</TD>
            <TD><input type="text" name="pages" value="1"></TD>
        </TR>
        <TR>
            <TD>Output File Name:</TD>
            <TD><input type="text" name="output_name" value="results"></TD>
        </TR>
        <TR>
            <TD>E-mail Address:</TD>
            <TD><input type="text" name="email_address" value="example@gmail.com">         
            </TD>
        </TR>
        <TR>
            <TD><input type="submit" value="Submit"></TD>
        </TR>
    </TABLE>
</form>
</body>
</html>

用于调用 python 脚本的示例 shell 脚本,该脚本将位于您的 cgi-bin 或其他指定的“可执行”允许目录中。

#!/bin/bash
# Runs the cgi-script, using the shell, using 'get' results from the index html form we parse it to the options in the python script.

echo "Content-type: text/html"
echo ""

echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'

echo '<title></title>'
echo '</head>'
echo '<body>'
query=`echo "$QUERY_STRING" | sed -n 's/^.*query=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
pages=`echo "$QUERY_STRING" | sed -n 's/^.*pages=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
output_name=`echo "$QUERY_STRING" | sed -n 's/^.*output_name=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
email_address=`echo "$QUERY_STRING" | sed -n 's/^.*email_address=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
echo '<h1>'
echo 'Running...'
echo '</h1>'
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd "$DIR"
python main.py -query $query -pages $pages -o $output_name
echo ''

echo '</body>'
echo '</html>'

示例 Python 脚本。

从 shell 脚本调用:

#!/usr/bin/env python
from xgoogle.search import GoogleSearch
import urllib2, re, csv, os
import argparse

class ScrapeProcess(object):
emails = []  # for duplication prevention

def __init__(self, filename):
    self.filename  = filename
    self.csvfile   = open(filename, 'wb+')
    self.csvwriter = csv.writer(self.csvfile)

def go(self, query, pages):
    search = GoogleSearch(query)
    search.results_per_page = 10

    for i in range(pages):
        search.page = i
        results = search.get_results()
        for page in results:
            self.scrape(page)

def scrape(self, page):
    try:
        request = urllib2.Request(page.url.encode("utf8"))
        html    = urllib2.urlopen(request).read()
    except Exception, e:
        return

    emails = re.findall(r'([A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*)', html)

    for email in emails:
        if email not in self.emails:  # if not a duplicate
            self.csvwriter.writerow([page.title.encode('utf8'), page.url.encode("utf8"), email])
            self.emails.append(email)

parser = argparse.ArgumentParser(description='Scrape Google results for emails')
parser.add_argument('-query', type=str, default='test', help='a query to use for the Google search')
parser.add_argument('-pages', type=int, default=10, help='number of Google results pages to scrape')
parser.add_argument('-o', type=str, default='emails.csv', help='output filename')    

args   = parser.parse_args()
args.o = args.o+'.csv' if '.csv' not in args.o else args.o  # make sure filename has .csv extension

s = ScrapeProcess(args.o)
s.go(args.query, args.pages)

完整的工作示例位于此处: https ://github.com/mhenes/Google-EmailScraper

免责声明这是我的 git - 使用分叉项目来展示此功能。

于 2015-12-16T17:34:17.200 回答
-2

IronPython 可能是您正在寻找的解决方案:http: //ironpython.net/

通过以下链接中提供的教程和代码,您应该能够创建响应事件的 html 元素,例如您提到的 html“按钮”。

我一直在使用 IronPython,并且在对脚本的内部和外部调用方面取得了成功。下面的教程很可能包含您可能遇到的任何其他问题。

helloworld.html -

IronPython 警报示例,文档中的内部 python 脚本。

要在浏览器中开发 Python 应用程序,您只需要您喜欢的文本编辑器;所以打开它,创建一个 HTML 文件,引用 dlr.js,然后你可以使用 script-tags 来运行 Python 代码:

<html>
<head>
<script src="http://gestalt.ironpython.net/dlr-latest.js"
        type="text/javascript"></script>
</head>
<body>
<script type="text/python">
  window.Alert("Hello from Python")
</script>
</body>
</html>

复制文件

在 REPL 窗口中执行此操作,所以让我们在浏览器中打开一个;只需在页面中放置以下脚本标签:

from Microsoft.Scripting.Silverlight import Repl

if 'document' not in globals():
  import System
  document = System.Windows.Browser.HtmlPage.Document
if 'window' not in globals():
  import System
  window = System.Windows.Browser.HtmlPage.Window

class PythonRepl(object):
  __ironpython__ = 'silverlightDlrRepl1'
  __minimize__ = 'silverlightDlrWindowLink'
  __container__ = 'silverlightDlrWindowContainer'

  def __init__(self):
    self.repl = Repl.Show('python')

  def hide_all_panels(self):
    window.Eval("sdlrw.hideAllPanels(document.getElementById(\"%s\"))" % self.__minimize__)
  
  def show_panel(self, id):
    window.Eval("sdlrw.showPanel(\"%s\")" % id)
  
  def show_ironpython(self):
    self.show_panel(self.__ironpython__)

  def remove(self):
    document.Body.RemoveChild(document.silverlightDlrWindowContainer)

def show():
  prepl = PythonRepl()
  repl = prepl.repl
  import sys
  sys.stdout = repl.OutputBuffer
  sys.stderr = repl.OutputBuffer
  return prepl

if document.QueryString.ContainsKey('console'): 
  prepl = show()
  if document.QueryString['console'] == 'hide':
    prepl.hide_all_panels()
  else:
    prepl.show_ironpython()

dom.py

IronPython 示例:用于添加 DOM 元素并将其 HTML 内容更改为“哎呀!” 点击时:

dir(document) 
div = document.CreateElement("div")
div.innerHTML = "Hello from Python!"
document.Body.AppendChild(div)
div.id = "message"
div.SetStyleAttribute("font-size", "24px")
def say_ouch(o, e):
    o.innerHTML = "Ouch!"

document.message.events.onclick += say_ouch

注意事项:IronPython 需要 SilverLight,因此它只能与 FireFox 或 Safari 一起使用。

与您的问题相关的优秀教程:http: //jimmy.schementi.com/2010/03/pycon-2010-python-in-browser.html

于 2015-11-18T19:47:34.987 回答