Настройки CSFR
origins = [
"http://localhost",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
allow_headers=["*"]
)
Подключение к Mysql
pip install mysql-connector-python
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
@app.get("/users")
def read_users():
return get_users()
def get_users():
conn = mydb
cursor = conn.cursor()
query = "SELECT * FROM database"
cursor.execute(query)
rows = cursor.fetchall()
cursor.close()
conn.close()
return rows