example_flask / bmain.py
PandaArtStation's picture
Rename main.py to bmain.py
63fd3e6 verified
raw
history blame contribute delete
No virus
854 Bytes
from flask import Flask, request, jsonify
import requests
import os
app = Flask(__name__)
WEATHER_API_URL = "http://api.openweathermap.org/data/2.5/weather"
API_KEY = os.getenv('OPENWEATHER_API_KEY')
@app.route('/weather', methods=['GET'])
def get_weather():
city = request.args.get('city', '') # Получаем город из параметров запроса
params = {
'q': city,
'appid': API_KEY,
'units': 'metric' # Метрическая система (Цельсии)
}
response = requests.get(WEATHER_API_URL, params=params)
if response.status_code == 200:
return jsonify(response.json())
else:
return jsonify({'error': 'Не удалось получить прогноз погоды'}), 500
if __name__ == '__main__':
app.run(host="0.0.0.0",port=7860,debug=False)