写一段python程序启动2个接口
时间: 2024-01-30 13:02:37 浏览: 117
好的,下面是一个简单的 Python 程序,可以启动两个接口:
```python
from flask import Flask
app = Flask(__name__)
@app.route('/api/v1/hello')
def hello():
return 'Hello World! This is the first API.'
@app.route('/api/v1/greeting')
def greeting():
return 'Greetings! This is the second API.'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
```
这段程序使用 Flask 框架创建了两个接口,分别是 `/api/v1/hello` 和 `/api/v1/greeting`。当访问这两个接口时,程序会返回不同的字符串。最后,程序会在本地启动一个 Web 服务器,监听在 `0.0.0.0:5000` 地址上。你可以通过访问 `http://localhost:5000/api/v1/hello` 和 `http://localhost:5000/api/v1/greeting` 来测试这两个接口。
相关问题
写一段python程序,使用flask框架写一个登录接口,输入用户名和密码登录。要求该接口尽可能支持高并发
好的,下面是一个简单的 Python 程序,使用 Flask 框架编写了一个支持高并发的登录接口:
```python
from flask import Flask, request, jsonify
from werkzeug.contrib.fixers import ProxyFix
import time
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app) # 配置反向代理
users = {'admin': 'admin123', 'guest': 'guest123'} # 用户名和密码
@app.route('/login', methods=['POST'])
def login():
data = request.get_json()
username = data.get('username')
password = data.get('password')
if not username or not password:
return jsonify({'code': 400, 'message': '用户名和密码不能为空!'})
# 模拟验证用户名和密码
if username in users and users[username] == password:
time.sleep(1) # 模拟处理时间
return jsonify({'code': 200, 'message': '登录成功!'})
else:
time.sleep(1) # 模拟处理时间
return jsonify({'code': 401, 'message': '用户名或密码错误!'})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, threaded=True)
```
这个程序中,我们创建了一个 `/login` 接口,用于处理用户登录请求。接口使用 POST 请求方式,需要在请求的 JSON 数据中包含 `username` 和 `password` 字段。接口会先验证用户名和密码是否为空,如果为空则返回错误信息。如果用户名和密码不为空,则模拟查询用户名和密码是否匹配,如果匹配则返回登录成功的信息,否则返回错误信息。
为了支持高并发,我们在启动 Flask 时设置了 `threaded=True`,使得 Flask 可以在多个线程中运行。另外,我们还使用了 Werkzeug 提供的 `ProxyFix` 中间件,用于处理反向代理的请求头。
需要注意的是,这个程序只是一个简单的示例,实际上在生产环境中,需要对用户输入进行更加严格的验证和安全性检查。
写一段python在cad画直线的代码
要在CAD中绘制直线,可以使用COM接口来与CAD进行交互。下面是一段使用Python语言编写的在CAD中画直线的代码:
```python
import win32com.client
# 连接CAD程序
acad = win32com.client.Dispatch("AutoCAD.Application")
# 获取当前文档
doc = acad.ActiveDocument
# 获取模型空间
msp = doc.ModelSpace
# 定义起点和终点坐标
start_point = win32com.client.VARIANT(win32com.client.VT_ARRAY | win32com.client.VT_R8, (0.0, 0.0, 0.0))
end_point = win32com.client.VARIANT(win32com.client.VT_ARRAY | win32com.client.VT_R8, (10.0, 10.0, 0.0))
# 绘制直线
line = msp.AddLine(start_point, end_point)
# 保存文档
doc.Save()
# 关闭CAD程序
acad.Quit()
```
需要注意的是,上述代码中使用了win32com模块来实现与CAD的交互。在运行代码前需要确保已经安装了pywin32库,以及CAD程序已经启动。
阅读全文
相关推荐
















