小编Him*_*rya的帖子

Traceback 中的 HTTP 方法无效:Uvicorn

我正在使用 uvicorn 0.11.8 和 fastapi 0.61.1。我的应用程序托管在 VPS 中。当我在本地服务器中运行该应用程序时,此类错误无法重现。它显示正确的消息 404 未找到方法不可用,但我无法弄清楚 VPS 中导致此问题的原因(回溯中的错误)。 在此输入图像描述

python linode digital-ocean fastapi uvicorn

8
推荐指数
1
解决办法
1万
查看次数

连接拒绝 Postgresql

这是我为使用 psycopg2 连接 Postgresql 编写的代码。我的 psql 和 pgadminIII 也在运行。

import psycopg2

connection = psycopg2.connect(dbname="gps_heatmap",user="postgres",host="localhost",password="1234")
cursor = connection.cursor()

cursor.execute("DROP TABLE IF EXISTS roads")
cursor.execute("CREATE TABLE roads (" +
                   "id SERIAL PRIMARY KEY," +
                   "name VARCHAR," + 
                   "centerline GEOMETRY)")
cursor.execute("CREATE INDEX ON roads USING GIST(centerline)")

connection.commit()
Run Code Online (Sandbox Code Playgroud)

但出现以下错误:

 OperationalError                          Traceback (most recent call last)
    <ipython-input-14-03e3f214b83e> in <module>()
          1 import psycopg2
          2 
    ----> 3 connection = psycopg2.connect(dbname="gps_heatmap",user="postgres",host="localhost",password="1234",port="5432")
          4 cursor = connection.cursor()
          5 

C:\Users\*******\Anaconda3\lib\site-packages\psycopg2\__init__.py in connect(dsn, database, user, password, host, port, connection_factory, cursor_factory, async, **kwargs) …
Run Code Online (Sandbox Code Playgroud)

python postgresql

5
推荐指数
1
解决办法
2万
查看次数

为带有别名的可选字段创建 pydantic 模型

具有别名的必填字段的 Pydantic 模型创建如下

class MedicalFolderUpdate(RWModel):
    id : str = Field(alias='_id')
    university : Optional[str]
Run Code Online (Sandbox Code Playgroud)

如何添加可选字段大学的别名“学校”,就像 id 一样?

python pydantic

5
推荐指数
1
解决办法
6231
查看次数

已安装Geohash模块但导入错误

我在Windows 10中使用ipython Jupyter笔记本.我已经在cmd中使用pip install geohash安装了Geohash.当我尝试使用geohash(import geohash)时出现以下错误:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-7-840910eb012f> in <module>()
----> 1 import geohash

ImportError: No module named 'geohash'
Run Code Online (Sandbox Code Playgroud)

再次检查是否安装了geohash,然后在我尝试重新安装时发出以下消息:

C:\Users\Himal Acharya>pip install geohash
Requirement already satisfied (use --upgrade to upgrade): geohash in c:\users\himal acharya\anaconda3\lib\site-packages
Requirement already satisfied (use --upgrade to upgrade): docutils>=0.3 in c:\users\himal acharya\anaconda3\lib\site-packages (from geohash)
Run Code Online (Sandbox Code Playgroud)

我再次卸载geohash.我从github手动安装geohash但出现同样的问题:导入错误

ipython geohashing

4
推荐指数
2
解决办法
4250
查看次数

将 fastapi 响应头中的服务器名称替换为假服务器名称

我使用 uvicorn 作为服务器来使用快速 api 运行应用程序。在 Swagger 中执行端点 url 时,服务器响应的响应标头中显示以下消息。

 content-length: 122 
 content-type: application/json 
 date: Sat12 Dec 2020 10:18:55 GMT 
 server: uvicorn 
Run Code Online (Sandbox Code Playgroud)

如何将服务器名称更改为新名称服务器:firstproject?以下代码将服务器名称 unciorn 与新名称连接起来

@app.middleware("http")
async def add_custom_header(request, call_next):
    response = await call_next(request)
    response.headers['server'] = 'firstproject'
    return response
Run Code Online (Sandbox Code Playgroud)

这给出了以下输出

content-length: 122 
 content-type: application/json 
 date: Sat12 Dec 2020 10:19:33 GMT 
 server: uvicornfirstproject 
Run Code Online (Sandbox Code Playgroud)

如何将响应标头中的服务器名称更改为 server :firstproject ?

编辑

在start_server.py中

 import uvicorn

from app.main import app

if __name__ == "__main__":
    uvicorn.run("start_server:app --header server:firstproject", host="0.0.0.0", port=8000, reload=True)

    
Run Code Online (Sandbox Code Playgroud)

给出以下错误

INFO:     Uvicorn running on http://0.0.0.0:8000 (Press …
Run Code Online (Sandbox Code Playgroud)

python-3.x fastapi

4
推荐指数
2
解决办法
4144
查看次数

BufferedWriter不写入文件

我必须从用户获取用户的名称和地址,并将其放入textfile.我写下面的代码:

package selfTest.nameAndAddress;

import com.intellij.codeInsight.template.postfix.templates.SoutPostfixTemplate;

import java.io.*;
import java.util.Arrays;

/**
 * Created by 
 */
public class Test {
    public static void main(String[] args) throws IOException {
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);


        //creating addressbook text file
        File fl=new File("E:/addressbook.txt");
        fl.createNewFile();

        FileReader fr=new FileReader(fl);
        BufferedReader in=new BufferedReader(fr);



        boolean eof=false;
        int inChar=0;

        String[] name=new String[2];
        String[] address=new String[2];

        int counter=0;


        do{
            FileWriter fw=new FileWriter(fl);
            BufferedWriter out=new BufferedWriter(fw);

            System.out.println("Enter "+(counter+1)+" students name "+" and address");

            name[counter]=br.readLine();
            address[counter]=br.readLine();

            out.write(name[counter]);
            System.out.println("Nmae: "+name[counter]+" ddress: "+address[counter]);
            counter++; …
Run Code Online (Sandbox Code Playgroud)

java bufferedwriter

2
推荐指数
1
解决办法
144
查看次数

conda 更新后没有名为“conda”的模块

我已经在我的 ubuntu 机器上安装了 miniconda3。当我使用 conda 时,它给出以下错误:

 File "/home/himal/miniconda3/bin/conda", line 12, in <module>
    from conda.cli import main
ModuleNotFoundError: No module named 'conda'
Run Code Online (Sandbox Code Playgroud)

但是虚拟环境(miniconda3/envs/himal)中的 jupyter Notebook 中的代码可以工作。我在虚拟环境“himal”中安装新软件包时遇到问题。如何更改Ubuntu的基础环境?

在此输入图像描述

编辑

的输出

$ echo ~/miniconda3/lib/python*/site-packages/conda-* 
Run Code Online (Sandbox Code Playgroud)

/home/himal/miniconda3/lib/python3.7/site-packages/conda-4.6.1-py3.7.egg-info
Run Code Online (Sandbox Code Playgroud)

~/miniconda3/bin/python --version
Run Code Online (Sandbox Code Playgroud)

Python 3.8.2
Run Code Online (Sandbox Code Playgroud)

虚拟环境中的 Python 版本是 3.7.2。当我使用 Pycharm 虚拟环境中的 jupyter 笔记本时,可以正常工作。但是从终端使用 conda 时出现问题

ubuntu conda miniconda

2
推荐指数
1
解决办法
6620
查看次数

Fastapi 中的速率限制

如何在 Fastapi 应用程序中对 API 端点请求进行速率限制?我需要为每个用户每秒限制 API 调用 5 个请求,并且超过该限制会阻止该特定用户 60 秒。

在 main.py

def get_application() -> FastAPI:
     application = FastAPI(title=PROJECT_NAME, debug=DEBUG, version=VERSION)
     application.add_event_handler(
        "startup", create_start_app_handler(application))
     application.add_event_handler(
        "shutdown", create_stop_app_handler(application))
     return application
app = get_application()
Run Code Online (Sandbox Code Playgroud)

在事件.py

def create_start_app_handler(app: FastAPI) -> Callable:  
    async def start_app() -> None:           

        redis = await aioredis.create_redis_pool("redis://localhost:8080")
        FastAPILimiter.init(redis)
    return start_app
Run Code Online (Sandbox Code Playgroud)

在端点

@router.post('/user',
             tags=["user"],
             name="user:user", dependencies=[Depends(RateLimiter(times=5, seconds=60))])
***code****
Run Code Online (Sandbox Code Playgroud)

从此文件 test.py 运行。

import uvicorn

from app.main import app

if __name__ == "__main__":
    uvicorn.run("test:app", host="0.0.0.0", port=8000, reload=True)
Run Code Online (Sandbox Code Playgroud)

我如上编辑,但出现以下错误。

File "****ite-packages\starlette\routing.py", line 526, …
Run Code Online (Sandbox Code Playgroud)

python rate-limiting fastapi

-3
推荐指数
3
解决办法
3584
查看次数