小编Ale*_*enz的帖子

浏览器在后续请求中不发送cookie

使用cookie进行用户识别的项目.

当用户到达时,它会调用该服务(在localhost中运行),并且带有响应头的服务发送cookie如下所示:

curl 'http://127.0.0.1:8000/api/v1.0/tracking' -X OPTIONS -H 'Access-Control-Request-Method: POST' -H 'Origin: http://local.com:8080' -H 'Access-Control-Request-Headers: content-type,x-forwarded-for' --compressed
Run Code Online (Sandbox Code Playgroud)

响应标头如下所示:

HTTP/1.1 200 OK
Connection: keep-alive
Keep-Alive: 60
Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, x-forwarded-for
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, PATCH, GET
Content-Length: 0
Content-Type: text/plain; charset=utf-8
Set-Cookie: id=random_id_123_123; expires=Wed, 06-Dec-2017 10:57:36 GMT; Domain=.local.com; Path=/
Run Code Online (Sandbox Code Playgroud)

然后在特定用户操作后,应用程序发送以下API请求:

curl 'http://127.0.0.1:8000/api/v1.0/tracking?event=video_added&user_id=123123123' -H 'Origin: http://local.com:8080' -H 'Accept: */*' -H 'Referer: http://local.com:8080/' -H 'Connection: keep-alive' --compressed
Run Code Online (Sandbox Code Playgroud)

上述请求的请求标头如下所示:

GET api/v1.0/tracking?event=video_added&user_id=123123123 HTTP/1.1
Host: 127.0.0.1:8000
Connection: keep-alive
Accept: */*
Origin: http://local.com:8080
User-Agent: My user agent
Referer: …
Run Code Online (Sandbox Code Playgroud)

python cookies python-3.x web sanic

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

在使用net/http/httptest时,Golang测试基于gin的REST API没有获取参数

我正在开发基于Gin golang的REST API,端点如下所示:

func carsByType(c *gin.Context) {
    fmt.Println("Go Request in Handler...")
    carType := c.Params.ByName("type")
    fmt.Println(carType)
    if carType != "" {

    }
    c.JSON(http.StatusBadRequest, gin.H{"result": "Bad request"})
    return
}

func main() {
    router := gin.Default()
    router.GET("/cars/:type", carsByType)
    router.Run(":80")
}
Run Code Online (Sandbox Code Playgroud)

当我通过浏览器和cURL向端点发出请求时,它正常工作,获取carType值,但是当我运行测试时,返回的错误请求和获取carType为"".

为了测试端点,我的测试代码如下所示:

func TestGetcarsByType(t *testing.T) {
    gin.SetMode(gin.TestMode)
    handler := carsByType
    router := gin.Default()
    router.GET("/cars/1", handler)

    req, err := http.NewRequest("GET", "/cars/1", nil)
    if err != nil {
        fmt.Println(err)
    }
    resp := httptest.NewRecorder()
    router.ServeHTTP(resp, req)
    assert.Equal(t, resp.Code, 200)
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

testing rest go go-gin

6
推荐指数
1
解决办法
2437
查看次数

使用gunicorn运行aiohttp服务器

我正在尝试使用Gunicorn运行基于aiohttp的服务器.

这是命令:

gunicorn aiohttpdemo_polls:app --bind 127.0.0.1:8080
Run Code Online (Sandbox Code Playgroud)

它返回:

Failed to find application: 'aiohttpdemo_polls' 
Run Code Online (Sandbox Code Playgroud)

但是当我使用python -m运行它时,如下所示:

python -m aiohttpdemo_polls
Run Code Online (Sandbox Code Playgroud)

它工作正常.代码可以在这里找到,这是aiohttp repo中的一个演示应用程序.也尝试过如下:

gunicorn aiohttpdemo_polls.main:app --bind 127.0.0.1:8080
Run Code Online (Sandbox Code Playgroud)

但它也没有运行服务器.它回来了

Failed to find application: 'aiohttpdemo_polls.main'
Run Code Online (Sandbox Code Playgroud)

知道在哪里进一步寻找解决问题的方法吗?

python python-3.x gunicorn aiohttp

6
推荐指数
2
解决办法
1119
查看次数

如何根据某些标准"循环"单个迭代?

我有一个字典列表,我想循环排序.

sample = [
    {'source': 'G', '"serial"': '0'},
    {'source': 'G', '"serial"': '1'},
    {'source': 'G', '"serial"': '2'},
    {'source': 'P', '"serial"': '30'},
    {'source': 'P', '"serial"': '0'},
    {'source': 'P', '"serial"': '1'},
    {'source': 'P', '"serial"': '2'},
    {'source': 'P', '"serial"': '3'},
    {'source': 'T', '"serial"': '2'},
    {'source': 'T', '"serial"': '3'}
]
Run Code Online (Sandbox Code Playgroud)

我想要这个结果:

sample_solved = [
    {'source': 'G', '"serial"': '0'},
    {'source': 'P', '"serial"': '30'},
    {'source': 'T', '"serial"': '2'},
    {'source': 'G', '"serial"': '1'},
    {'source': 'P', '"serial"': '1'},
    {'source': 'T', '"serial"': '3'},
    {'source': 'G', '"serial"': '2'},
    {'source': 'P', '"serial"': …
Run Code Online (Sandbox Code Playgroud)

python sorting algorithm data-structures python-3.x

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

在mongodb嵌入式文档中添加新属性(在最新的数组项中)

我有如下的mongo文件:

{
        "_id" : ObjectId("59ccb655071d4c2ceebe190c"),
        "session_deatils" : [
                {
                        "session_start" : 1,
                        "session_complete" : 1,
                        "started_at" : ISODate("2017-09-28T08:42:19.770Z"),
                        "event_count" : 2
                },
                {
                        "session_start" : 1,
                        "session_complete" : 1,
                        "started_at" : ISODate("2017-09-28T08:53:08.618Z"),
                        "event_count" : 1
                },
                {
                        "session_start" : 1,
                        "session_complete" : 1,
                        "started_at" : ISODate("2017-09-28T09:19:42.726Z")
                }
        ],
        "session_id" : "12312312313123",
}
Run Code Online (Sandbox Code Playgroud)

我想在会话详细信息的最新项目中添加新字段和值,例如“ event_count”

        {
                "session_start" : 1,
                "session_complete" : 1,
                "started_at" : ISODate("2017-09-28T09:19:42.726Z")
        }

            and I want to update it and the array element should look like below:

        { …
Run Code Online (Sandbox Code Playgroud)

mongodb pymongo mongodb-query aggregation-framework pymongo-3.x

5
推荐指数
0
解决办法
134
查看次数