mirror of
https://github.com/gnh1201/caterpillar.git
synced 2025-09-05 17:31:03 +00:00
Create web.py
This commit is contained in:
parent
d9b6600302
commit
13f06f130c
87
web.py
Normal file
87
web.py
Normal file
|
@ -0,0 +1,87 @@
|
|||
from flask import Flask, request, redirect, url_for, render_template
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import importlib
|
||||
|
||||
import hashlib
|
||||
from decouple import config
|
||||
|
||||
from base import Extension, jsonrpc2_create_id, jsonrpc2_result_encode, jsonrpc2_error_encode
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['UPLOAD_FOLDER'] = 'data/'
|
||||
|
||||
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
||||
os.makedirs(app.config['UPLOAD_FOLDER'])
|
||||
|
||||
@app.route('/')
|
||||
def upload_form():
|
||||
return render_template('upload.html')
|
||||
|
||||
@app.route('/upload', methods=['POST'])
|
||||
def process_upload():
|
||||
# make connection profile from Flask request
|
||||
conn = Connection(request)
|
||||
|
||||
# pass to the method
|
||||
method = request.form['method']
|
||||
filename = request.files['file'].filename
|
||||
params = {
|
||||
'filename': filename
|
||||
}
|
||||
|
||||
# just do it
|
||||
return Extension.dispatch_rpcmethod(method, 'call', '', params, conn)
|
||||
|
||||
@app.route('/jsonrpc2', methods=['POST'])
|
||||
def process_jsonrpc2():
|
||||
# make connection profile from Flask request
|
||||
conn = Connection(request)
|
||||
|
||||
# JSON-RPC 2.0 request
|
||||
jsondata = request.get_json(silent=True)
|
||||
if jsondata['jsonrpc'] == "2.0":
|
||||
return Extension.dispatch_rpcmethod(jsondata['method'], 'call', jsondata['id'], jsondata['params'], conn)
|
||||
|
||||
# when error
|
||||
return jsonrpc2_error_encode({
|
||||
'message': "Not vaild JSON-RPC 2.0 request"
|
||||
})
|
||||
|
||||
def jsonrpc2_server(conn, id, method, params):
|
||||
return Extension.dispatch_rpcmethod(method, "call", id, params, conn)
|
||||
|
||||
class Connection():
|
||||
def send(self, data):
|
||||
self.messages.append(data)
|
||||
|
||||
def recv(self):
|
||||
print ("Not allowed")
|
||||
|
||||
def close(self):
|
||||
print ("Not allowed")
|
||||
|
||||
def __init__(self, req):
|
||||
self.messages = []
|
||||
self.request = req
|
||||
|
||||
if __name__ == "__main__":
|
||||
# initalization
|
||||
try:
|
||||
listening_port = config('PORT', default=5555, cast=int)
|
||||
client_encoding = config('CLIENT_ENCODING', default='utf-8')
|
||||
except KeyboardInterrupt:
|
||||
print("\n[*] User has requested an interrupt")
|
||||
print("[*] Application Exiting.....")
|
||||
sys.exit()
|
||||
except Exception as e:
|
||||
print("[*] Failed to initialize:", str(e))
|
||||
|
||||
# set environment of Extension
|
||||
Extension.set_protocol('http')
|
||||
|
||||
# load extensions
|
||||
#Extension.register(importlib.import_module("plugins.yourownplugin").YourOwnPlugin())
|
||||
|
||||
app.run(debug=True, host='0.0.0.0', port=listening_port)
|
Loading…
Reference in New Issue
Block a user