要从远程机器触发一个Python脚本,而不使用SSH,可以使用以下方法之一:
from flask import Flask, request
app = Flask(__name__)
@app.route('/trigger', methods=['POST'])
def trigger_script():
# 执行Python脚本的代码
# ...
return 'Script triggered successfully'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
在远程机器上运行该脚本后,您可以从其他机器上使用HTTP POST请求触发脚本的执行。例如,使用Python的requests
库:
import requests
url = 'http://remote-machine-ip:8080/trigger'
response = requests.post(url)
print(response.text)
import pika
def callback(ch, method, properties, body):
# 执行Python脚本的代码
# ...
ch.basic_ack(delivery_tag=method.delivery_tag)
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='script_queue')
channel.basic_consume(queue='script_queue', on_message_callback=callback)
channel.start_consuming()
在其他机器上发送消息到队列的代码示例:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('remote-machine-ip'))
channel = connection.channel()
channel.queue_declare(queue='script_queue')
channel.basic_publish(exchange='', routing_key='script_queue', body='path/to/script.py')
connection.close()
这样,当在其他机器上发送消息到队列时,远程机器上的消费者程序将从队列中获取到消息,并执行相应的脚本。
请注意,以上示例仅提供了简化的代码示例,并且可能需要根据您的具体需求进行进一步的修改和完善。