如何与web运行python脚本进行简单的交互?

时间:2016-02-15 11:14:02

标签: python html web

我在Python中有一个运行循环脚本,我希望与HTML页面进行交互。例如:

  

(HTML)点击按钮 - >魔术 - > (Python)做一些功能   script.py

This response不适合此。

2 个答案:

答案 0 :(得分:0)

可能你可以使用Selenium python绑定来与你的python脚本进行网页交互。

Selenium link

示例:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

答案 1 :(得分:0)

我在python中通过PIPE解决了这个问题。 Python脚本按行读取文件 p1.txt ,server.js发送文本。例如,这里server.js发送字符串'COMMAND',我们收到了script.py响应。

  

script.py

import os,sys
fd = "./p1.txt"
try:
    os.mkfifo(fd)
except OSError:
    pass
rp = os.open(fd, os.O_RDONLY)

#Need to create new thread, for background reading file
while True:
    response = os.read(rp, 54)
    if response == "COMMAND":
        #do
        print 'ICATCHYOU'
  

server.js

var fs = require('fs');
var path = '/home/pi/develop/p1.txt';

var streamOptions = { flags: 'w',
                      encoding: 'utf-8',
                      mode: 0666 };

var afterOpen = function(err, fd) {
    var writeStream = fs.createWriteStream(path, streamOptions);   
    writeStream.write("COMMAND");
};


fs.open(path, 'w', afterOpen);