Click here to Skip to main content
15,358,692 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, coding guys,

As you saw in the title, my Python API returns a 404 page when a page is found, like /python/ should return "print("Hello, world!")", but returns 404 instead. This is my code:

main.py
from flask import Flask, send_from_directory, abort
import json
from flask_cors import CORS
app = Flask("app")
CORS(app)

@app.route("/")
def main():
	return abort(400)

@app.route("/<string:lang>/")
def hwres(lang):
	if lang.split("/")[0] == lang:
		lang = lang.split("\\")[0]
	else:
		lang = lang.split("/")[0]
	lang = lang.lower()
	with open("examples.json", "r") as exampleIO:
		global exampleJson, exampleJsonStr
		exampleJsonStr = exampleIO.read()
		exampleJson = json.loads(exampleJsonStr)
	try:
		str(exampleJson["hw"][lang])
	except:
		return abort(400)
	with open("hw-code.txt", "r+") as exampleIO:
		exampleIO.truncate()
	with open("hw-code.txt", "a") as exampleIO:
		exampleIO.write(exampleJson["hw"][lang])
	return send_from_directory(".", "hw-code.json")

app.run(host="0.0.0.0", port=8080)


Abou the main site returning Bad Request, that's supposed to happen.

What I have tried:

                              
Posted
Comments
Richard MacCutchan 18-May-22 11:07am
   
You have not explained which line causes the error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900