python OnCom.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python OnCom.py相关的知识,希望对你有一定的参考价值。

import json
import random
import string
import urllib2
from flask import Flask, render_template, request, jsonify
from flask.ext.mysqldb import MySQL
from flask.ext.cors import CORS, cross_origin

app = Flask(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'
app.debug = True
mysql = MySQL()

# MySQL configurations
app.config['MYSQL_HOST'] = '10.151.34.138'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'terserah'
app.config['MYSQL_DB'] = 'awan'
mysql.init_app(app)

CORS(app, resources={r"/judge": {"origins": "localhost"}})

@app.route('/judge', methods=['POST'])
@app.route('/judge/<string_id>', methods=['GET'])
@cross_origin(origin='localhost', headers=['Content- Type', 'Authorization'])
def judge(string_id='xxxxxxxxxx'):
    if request.method == 'POST':
        data = json.loads(request.data.decode())

        sourcecode= str(data["sourcecode"]).encode('string_escape')
        stdin = str(data["stdin"]).encode('string_escape')
        timelimit = data["timelimit"]
        lang_id = data["lang_id"]
        networking = "0"
        string_id = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))

        cursor = mysql.connection.cursor()
        query = "INSERT INTO inputs (sourcecode,stdin,timelimit,lang_id,networking,string_id) VALUES ('"+sourcecode+"', '"+stdin+"' , '"+timelimit+"' , '"+lang_id+"' , '"+networking+"' , '"+string_id+"')"
        cursor.execute(query)
        mysql.connection.commit()

        #urllib2.urlopen("http://10.151.34.171:3000/judge/"+string_id).read()
        
        return string_id

    if request.method == 'GET':
        query = "SELECT * FROM ouputs WHERE "
        cur = mysql.connection.cursor()
        cur.execute('''SELECT * FROM inputs''')
        rv = cur.fetchall()
        return str(rv)
        return jsonify(stdout="ini stdout", stderr="ini stderr")
        #return 'xxxxxxxxxx'
        #return string_id


@app.route('/')
def hello_world():
    cur = mysql.connection.cursor()
    cur.execute('''SELECT * FROM inputs''')
    rv = cur.fetchall()
    return str(rv)


@app.route('/code', methods=['GET', 'POST'])
def code():
    if request.method == 'POST':
        input_code = request.data
        print input_code
        return jsonify(result="ok, masuk")
    else:
        return render_template('index.html')




if __name__ == '__main__':
    app.run(host='0.0.0.0')

以上是关于python OnCom.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python