Procházet zdrojové kódy

上传文件至 'be'

pull/1/head
gitea.shuishan před 3 roky
rodič
revize
030d2d2d47
4 změnil soubory, kde provedl 345 přidání a 38 odebrání
  1. +47
    -38
      be/app.py
  2. +42
    -0
      be/my_check_logic_error.py
  3. +127
    -0
      be/mysubmit.py
  4. +129
    -0
      be/testcase.py

+ 47
- 38
be/app.py Zobrazit soubor

@ -4,16 +4,19 @@ from flask import request
import json
import pymysql
import sys
import mysubmit
import testcase
import my_check_logic_error
sys.path.append('../')
from PIPE.main import PIPEModel
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
db = pymysql.connect(host='127.0.0.1',
db = pymysql.connect(host='192.168.0.89',
port=3306,
user='shuishan',
password='Shuishan@2020',
user='root',
password='2020yunjisuan!',
db='shuishan',
charset='utf8')
@ -41,54 +44,58 @@ def get_problem_description(_id: str):
"from problem where id = %d" % int(_id))
des = cursor.fetchone()
# print(des)
title = '<div><strong style="font-size:22px;color:blue;">%s. %s</strong></div><br/>' % (_id, des[0])
description = '<div><strong style="font-size:20px;color:blue;">Description</strong></div><div>%s</div><br/>' % des[1]
input_description = '<div><strong style="font-size:20px;color:blue;">Input Description</strong></div><div>%s</div><br/>' \
title = '<div><strong style="font-size:22px">%s. %s</strong></div><br/>' % (_id, des[0])
description = '<div><strong style="font-size:20px">Description</strong></div><div>%s</div><br/>' % des[1]
input_description = '<div><strong style="font-size:20px">Input Description</strong></div><div>%s</div><br/>' \
% des[2]
output_description = '<div><strong style="font-size:20px;color:blue;">Output Description</strong></div><div>%s</div><br/>' \
output_description = '<div><strong style="font-size:20px">Output Description</strong></div><div>%s</div><br/>' \
% des[3]
samples = json.loads(des[-1].replace("\n", "\\n"))
samples_html = ''
for s in samples:
_input = s["input"].strip().replace(" ", "&nbsp;&nbsp;").replace("\n", "<br/>&nbsp;&nbsp;&nbsp;&nbsp;")
_output = s["output"].strip().replace(" ", "&nbsp;&nbsp;").replace("\n", "<br/>&nbsp;&nbsp;&nbsp;&nbsp;")
# _input = s["input"].strip().replace(" ", "&nbsp;&nbsp;")
# _output = s["output"].strip().replace(" ", "&nbsp;&nbsp;")
stra = '<div>'
strb = '<div style="width:100%;">'
strc = '<div style="display:inline-block;width:45%;vertical-align:top;"><strong style="font-size:20px;color:blue;">Input</strong></div>'
strd = '<div style="display:inline-block;width:2%;vertical-align:top;"></div>'
stre = '<div style="display:inline-block;width:45%;vertical-align:top;"><strong style="font-size:20px;color:blue;">Output</strong></div>'
strf = '</div><br/>'
strg = '<div style="width:100%;">'
strh = '<div style="display:inline-block;width:45%;height:100%;vertical-align:top;border:0.5px solid;border-radius:5px;"><br/>&nbsp;&nbsp;&nbsp;&nbsp;' + _input + '<br/>&nbsp;&nbsp;&nbsp;&nbsp;</div>'
stri = '<div style="display:inline-block;width:2%;height:100%;vertical-align:top;"></div>'
strj = '<div style="display:inline-block;width:45%;height:100%;vertical-align:top;border:0.5px solid;border-radius:5px;"><br/>&nbsp;&nbsp;&nbsp;&nbsp;' + _output + '<br/>&nbsp;&nbsp;&nbsp;&nbsp;</div>'
strk = '</div>'
strl = '</div><br/>'
samples_html = samples_html + stra + strb + strc + strd + stre + strf + strg + strh + stri + strj + strk + strl
return title + description + input_description + output_description + samples_html, 200
_input = s["input"].strip().replace(" ", "&nbsp;&nbsp;").replace("\n", "<br/>")
_output = s["output"].strip().replace(" ", "&nbsp;&nbsp;").replace("\n", "<br/>")
str1 = '<div>'
str2 = '<div style="display:inline-block;width:45%;vertical-align:top;">'
str3 = '<div><strong style="font-size:20px">Input</strong></div><br/>'
str4 = '<div>' + _input + '</div>'
str5 = '</div>'
str6 = '<div style="display:inline-block;width:45%;vertical-align:top;">'
str7 = '<div><strong style="font-size:20px">Output</strong></div><br/>'
str8 = '<div>' + _output + '</div>'
str9 = '</div>'
str0 = '</div><br/>'
samples_html = samples_html + str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8 + str9 + str0
return title + description + input_description + output_description + samples_html, 200
@app.route('/case_test/', methods=['POST'])
def case_test_():
pid: str = request.json.get("pid")
code: str = request.json.get("code")
print(pid)
print(code)
return 'pass', 200
with open('wulin.txt', 'w', encoding='utf-8', newline='') as f:
f.write(pid)
f.close()
results = testcase.submit_handler(code, pid, 'C')
return results, 200
@app.route('/check-logic-error/', methods=['POST'])
def check_logic_error():
pid: str = request.json.get("pid")
code: str = request.json.get("code")
print(pid)
print(code)
model = PIPEModel()
model.load_savedModel()
answer = model.predict_code(code=code)
del model
# print(type(code))
# model = PIPEModel()
# model.load_savedModel('./PIPE/training/C2AE_model/cp-0015.ckpt')
# answer = model.predict_code(code=code)
# del model
# with open('wulin3.txt', 'w', encoding='utf-8', newline='') as f: # 设置文件对象
# f.write(answer) # 将字符串写入文件中
# f.close()
answer = my_check_logic_error.check_logic_error()
# results = 'check_logic_error'
return answer, 200
@ -97,12 +104,14 @@ def check_logic_error():
def submit():
pid: str = request.json.get("pid")
code: str = request.json.get("code")
print(pid)
print(code)
with open('wulin2.txt', 'w', encoding='utf-8', newline='') as f:
f.write(pid)
f.close()
results = mysubmit.submit_handler(code, pid, 'C')
# results = check_logic_error(pid, code)
results = 'submit'
return results, 200
if __name__ == '__main__':
app.run()
app.run(host='0.0.0.0')

+ 42
- 0
be/my_check_logic_error.py Zobrazit soubor

@ -0,0 +1,42 @@
from urllib import parse
import requests
# 必须submit/test之后才能checklogic error (test即一次submit)
# 从submit_id.txt中找到submit_id,填入referer
# 从data.c找到代码,编码之后,填入url
# get请求
# 获得返回结果
def check_logic_error() :
file = open('data.c', mode='r', encoding='utf-8', newline='')
code = file.read()
file.close()
code = parse.quote(code)
print(code)
url = "http://jf.shuishan.net.cn/api/code2vec?code=" + code
print(url)
file = open('submit_id.txt', mode='r', encoding='utf-8', newline='')
submit_id = file.read()
file.close()
payload={}
headers = {
'Host': 'jf.shuishan.net.cn',
'Connection': 'keep-alive',
'Accept': 'application/json, text/plain, */*',
'X-CSRFToken': '6eM4EeOzqLJFt6xfkbSMQD1e89qF28Lgg5aPIbB1k7Hvu6UFVzzb2d1u5lQG37p6',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36',
'Content-Type': 'application/json;charset=utf-8',
'Referer': 'http://jf.shuishan.net.cn/status/'+ submit_id,
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cookie': '_ga=GA1.3.1580759526.1611565238; _gid=GA1.3.412268444.1611565238; csrftoken=6eM4EeOzqLJFt6xfkbSMQD1e89qF28Lgg5aPIbB1k7Hvu6UFVzzb2d1u5lQG37p6; sessionid=0e47tlihcjyqr3oynpx5pq9xpf7jtqml; sessionid=0e47tlihcjyqr3oynpx5pq9xpf7jtqml'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
with open('check_logic_error.txt', 'w', encoding='utf-8', newline='') as f: # 设置文件对象
f.write(response.text) # 将字符串写入文件中
f.close()
return response.text

+ 127
- 0
be/mysubmit.py Zobrazit soubor

@ -0,0 +1,127 @@
import requests
import re
import time
def submit(payload) :
url = "http://jf.shuishan.net.cn/api/submission"
payload = payload
headers = {
'Host': 'jf.shuishan.net.cn',
'Connection': 'keep-alive',
'Content-Length': '101',
'Accept': 'application/json, text/plain, */*',
'X-CSRFToken': '6eM4EeOzqLJFt6xfkbSMQD1e89qF28Lgg5aPIbB1k7Hvu6UFVzzb2d1u5lQG37p6',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/88.0.4324.104 Safari/537.36',
'Content-Type': 'application/json;charset=UTF-8',
'Origin': 'http://jf.shuishan.net.cn',
'Referer': 'http://jf.shuishan.net.cn/problem/1-1',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cookie': '_ga=GA1.3.1580759526.1611565238; _gid=GA1.3.412268444.1611565238; csrftoken=6eM4EeOzqLJFt6xfkbSMQD1e89qF28Lgg5aPIbB1k7Hvu6UFVzzb2d1u5lQG37p6; sessionid=0e47tlihcjyqr3oynpx5pq9xpf7jtqml; _gat=1; sessionid=0e47tlihcjyqr3oynpx5pq9xpf7jtqml'
}
response = requests.request("POST", url, headers=headers, data=payload)
## print(response.text)
res = response.text
submit_id = re.findall(r'"submission_id": "([^"]+)"',res)
submit_id = submit_id[0]
with open('submit_id.txt', 'w', encoding='utf-8', newline='') as f: # 设置文件对象
f.write(submit_id) # 将字符串写入文件中
f.close()
#获取judge信息
url = "http://jf.shuishan.net.cn/api/submission?id="+str(submit_id)
#url = "http://jf.shuishan.net.cn/api/submission?id=2610feac243108d927e8760f24b808c6"
payload={}
headers = {
'Host': 'jf.shuishan.net.cn',
'Connection': 'keep-alive',
'Accept': 'application/json, text/plain, */*',
'X-CSRFToken': '6eM4EeOzqLJFt6xfkbSMQD1e89qF28Lgg5aPIbB1k7Hvu6UFVzzb2d1u5lQG37p6',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36',
'Content-Type': 'application/json;charset=utf-8',
'Referer': 'http://jf.shuishan.net.cn/problem/1-1',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cookie': '_ga=GA1.3.1580759526.1611565238; _gid=GA1.3.412268444.1611565238; csrftoken=6eM4EeOzqLJFt6xfkbSMQD1e89qF28Lgg5aPIbB1k7Hvu6UFVzzb2d1u5lQG37p6; sessionid=0e47tlihcjyqr3oynpx5pq9xpf7jtqml; _gat=1; sessionid=0e47tlihcjyqr3oynpx5pq9xpf7jtqml'
}
#等待judgeserver出结果
time.sleep(0.5)
response = requests.request("GET", url, headers=headers, data=payload)
## print(response.text)
res = response.text
result_code = re.findall(r'"result": ([^"]+),',res)
error_code = re.findall(r'"error": ([^"]+),',res)
result_code = result_code[0]
error_code = error_code[0]
## print(result_code)
## print(error_code)
if(error_code != '0' and error_code != "null"):
print("ERROR_CODE")
return "ERROR_CODE"
elif(result_code == '0'):
print("ACCEPTED")
return "ACCEPTED"
elif(result_code == '1'):
print("CPU_TIME_LIMIT_EXCEEDED")
return "CPU_TIME_LIMIT_EXCEEDED"
elif(result_code == '2'):
print("REAL_TIME_LIMIT_EXCEEDED")
return "REAL_TIME_LIMIT_EXCEEDED"
elif(result_code == '3'):
print("MEMORY_LIMIT_EXCEEDED")
return "MEMORY_LIMIT_EXCEEDED"
elif(result_code == '4'):
print("RUNTIME_ERROR")
return "RUNTIME_ERROR"
elif (result_code == '5'):
print("SYSTEM_ERROR")
return "SYSTEM_ERROR"
else:
print("WRONG_ANSWER")
return "WRONG_ANSWER"
#写入一个输出文件
def submit_handler(code, problem_id, language_type):
with open('data.c', 'w', encoding='utf-8', newline='') as f:
f.write(code)
f.close()
file = open('data.c', mode='r', encoding='utf-8', newline='')
code = file.readlines()
file.close()
code = ''.join([i.replace(' ', r'\t').replace('"', r'\"').rstrip() + r'\n' for i in code])
## print(code)
# payload="{\"problem_id\":1,\"language\":\"C\",\"code\":\"# include <stdio.h>\\nint main()\\n{\\n\\tint a;\\n\\treturn 0;\\n}\"}"
# print(payload)
payload = "{\"problem_id\":" + problem_id + ",\"language\":\"" + language_type + "\",\"code\":\"" + code + "\"}"
## print(payload)
results = submit(payload)
return results
# 输入文件名,题号,语言种类
# results = submit_handler('1.c','1','C')
# print(results)
#获取逻辑错误信息

+ 129
- 0
be/testcase.py Zobrazit soubor

@ -0,0 +1,129 @@
import requests
import re
import time
def submit(payload) :
url = "http://jf.shuishan.net.cn/api/submission"
payload = payload
headers = {
'Host': 'jf.shuishan.net.cn',
'Connection': 'keep-alive',
'Content-Length': '101',
'Accept': 'application/json, text/plain, */*',
'X-CSRFToken': '6eM4EeOzqLJFt6xfkbSMQD1e89qF28Lgg5aPIbB1k7Hvu6UFVzzb2d1u5lQG37p6',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/88.0.4324.104 Safari/537.36',
'Content-Type': 'application/json;charset=UTF-8',
'Origin': 'http://jf.shuishan.net.cn',
'Referer': 'http://jf.shuishan.net.cn/problem/1-1',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cookie': '_ga=GA1.3.1580759526.1611565238; _gid=GA1.3.412268444.1611565238; csrftoken=6eM4EeOzqLJFt6xfkbSMQD1e89qF28Lgg5aPIbB1k7Hvu6UFVzzb2d1u5lQG37p6; sessionid=0e47tlihcjyqr3oynpx5pq9xpf7jtqml; _gat=1; sessionid=0e47tlihcjyqr3oynpx5pq9xpf7jtqml'
}
response = requests.request("POST", url, headers=headers, data=payload)
## print(response.text)
res = response.text
submit_id = re.findall(r'"submission_id": "([^"]+)"',res)
submit_id = submit_id[0]
with open('submit_id.txt', 'w', encoding='utf-8', newline='') as f: # 设置文件对象
f.write(submit_id) # 将字符串写入文件中
f.close()
## print(submit_id)
#获取judge信息
url = "http://jf.shuishan.net.cn/api/submission?id="+str(submit_id)
#url = "http://jf.shuishan.net.cn/api/submission?id=2610feac243108d927e8760f24b808c6"
payload={}
headers = {
'Host': 'jf.shuishan.net.cn',
'Connection': 'keep-alive',
'Accept': 'application/json, text/plain, */*',
'X-CSRFToken': '6eM4EeOzqLJFt6xfkbSMQD1e89qF28Lgg5aPIbB1k7Hvu6UFVzzb2d1u5lQG37p6',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36',
'Content-Type': 'application/json;charset=utf-8',
'Referer': 'http://jf.shuishan.net.cn/problem/1-1',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cookie': '_ga=GA1.3.1580759526.1611565238; _gid=GA1.3.412268444.1611565238; csrftoken=6eM4EeOzqLJFt6xfkbSMQD1e89qF28Lgg5aPIbB1k7Hvu6UFVzzb2d1u5lQG37p6; sessionid=0e47tlihcjyqr3oynpx5pq9xpf7jtqml; _gat=1; sessionid=0e47tlihcjyqr3oynpx5pq9xpf7jtqml'
}
#等待judgeserver出结果
time.sleep(0.5)
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
res = response.text
result_code = re.findall(r'"result": ([^"]+),',res)
error_code = re.findall(r'"error": ([^"]+),',res)
result_code = result_code[1]
error_code = error_code[0]
print(result_code)
print(error_code)
if(error_code != '0' and error_code != "null"):
print("ERROR_CODE")
return "ERROR_CODE"
elif(result_code == '0'):
print("SUCCESS")
return "SUCCESS"
elif(result_code == '1'):
print("CPU_TIME_LIMIT_EXCEEDED")
return "CPU_TIME_LIMIT_EXCEEDED"
elif(result_code == '2'):
print("REAL_TIME_LIMIT_EXCEEDED")
return "REAL_TIME_LIMIT_EXCEEDED"
elif(result_code == '3'):
print("MEMORY_LIMIT_EXCEEDED")
return "MEMORY_LIMIT_EXCEEDED"
elif(result_code == '4'):
print("RUNTIME_ERROR")
return "RUNTIME_ERROR"
elif (result_code == '5'):
print("SYSTEM_ERROR")
return "SYSTEM_ERROR"
else:
print("WRONG_ANSWER")
return "WRONG_ANSWER"
#写入一个输出文件
def submit_handler(code, problem_id, language_type):
with open('data.c', 'w', encoding='utf-8', newline='') as f:
f.write(code)
f.close()
file = open('data.c', mode='r', encoding='utf-8', newline='')
code = file.readlines()
file.close()
code = ''.join([i.replace(' ', r'\t').replace('"', r'\"').rstrip() + r'\n' for i in code])
## print(code)
# payload="{\"problem_id\":1,\"language\":\"C\",\"code\":\"# include <stdio.h>\\nint main()\\n{\\n\\tint a;\\n\\treturn 0;\\n}\"}"
# print(payload)
payload = "{\"problem_id\":" + problem_id + ",\"language\":\"" + language_type + "\",\"code\":\"" + code + "\"}"
## print(payload)
results = submit(payload)
return results
# 输入文件名,题号,语言种类
#results = submit_handler('22.c','22','C')
#print(results)
#获取逻辑错误信息

Načítá se…
Zrušit
Uložit