降伏?決して!
時間後...私の最初のpythonスクリプトはまだ手を必要としていますが、これまでのところ私は見ることができますが、それは実行して元々はやりたいと思っていました。
これは一種のラッパーとサーバーであり、同時に強化される可能性のある妥当性とセキュリティチェックを含んでいますが、現在はカンマで区切られた単一のパラメータ、文字列(必要な名前サブプロセススクリプト)と、数値をパラメータとして使用します(sayhello,2000
など)。まだまだJSONが追加されていませんが、現在は必要ありません。
(それは後で必要になるように、異なるURLでテストし、まだ別のマシン上でテストされていません)これは、任意のPythonのフレームワークなしで動作しますが、てWSGIServerで、jQueryのAJAXとCORSとクライアント側
これによって、あなたは」 3つのファイルが見つかります。 ajax.html
、server.py
および検査スクリプトsayhello.py
。
事前のヒント、警告または改善をお寄せいただきありがとうございます。あなたがそれを好きだと思います。
ajax.html
<html>
<head>
<title>Ajax POST to python</title>
</head>
<body>
send message: <input type="text" id="message" value="" size="10" placeholder="string,number"> <input type=button id="send" value="send">
<br />
response: <span id="response"></span>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$("input#send").click(function(event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: 'http://localhost:8080',
crossDomain: true,
data: $('input#message').val(),
dataType: 'text',
timeout: 3000,
success: function(responseData, textStatus, jqXHR)
{
$('span#response').html(responseData);
},
error: function (responseData, textStatus, errorThrown)
{
console.warn(responseData, textStatus, errorThrown);
alert('failed - ' + textStatus);
}
});
});
</script>
</html>
#!/usr/bin/env python
import threading
from subprocess import Popen, PIPE
from pathlib import Path
from wsgiref.simple_server import make_server
PORT = 8080
REQUEST_METHOD = "POST"
def myapp(env, http_stuff):
if env['REQUEST_METHOD'] == REQUEST_METHOD:
try:
request_body_size = int(env['CONTENT_LENGTH'])
request_body = env['wsgi.input'].read(request_body_size)
except (TypeError, ValueError):
request_body = "0"
try:
#---------------------------security/plausibility
allowed_scripts = ["sayhello","count_apples","get_scale_weight","etc."]
el = request_body.split(",");
arg_filename = str(el[0].strip())
arg_num = int(el[1].strip())
if not (arg_filename in allowed_scripts):
arg_filename = ""
if isinstance(arg_num, int):
arg_num = str(arg_num)
else:
arg_num = ""
#---------------------------script exists ?
my_file = Path(arg_filename+".py")
if my_file.is_file():
output = "no valid output."
#---------------------------subprocess
proc = Popen(["python", arg_filename+".py",arg_num], shell=False, stdout=PIPE, stderr=PIPE)
output = proc.communicate()[0]
#---------------------------more stuff
response_body = output
else:
response_body = "["+request_body+"] is not allowed !"
except:
response_body = "some other error"
else:
response_body = "no POST data"
status = '200 OK'
#---------------------------header with CORS support
headers = [('Content-type', 'text/plain'),
("Access-Control-Allow-Origin", "*")]
http_stuff(status, headers)
return response_body
def start_server():
httpd = make_server("", PORT, myapp)
httpd.serve_forever()
if __name__ == "__main__":
start_server()
sayhello.py server.py
#!/usr/bin/env python
import sys
print "I am sayhello.py and I say HELLO "+sys.argv[1]
あなたはそれがローカル、静的なファイルを提供するために、その目的としてSimpleHTTPServerを、使用しないでください。あなたは、適切にwsgiアプリケーションを実装する必要があります。たぶんDjangoやFlaskのようなものを使用しています。 – spectras
このエンドポイントについて見つけたら誰かが簡単に 'main.py&rm -rf'を実行できるので、セキュリティ上の脆弱性について考えていることを願っています。そして私はこれに関する@spectrasに同意します。 DjangoやFlaskを使ってPython webappを開発してください。 –
Apacheを使いたいのはなぜですか? –