Using Python 3.9.1 and requests 2.25.1 a local connection to a Mongoose HTTP server takes 2 seconds

131 views Asked by At

I am writing a little REST API client using Python, Java and NodeJS. The server is written using the Mongoose HTTP server.

With Java and NodeJS every request takes only milliseconds but with Python every request takes 2 seonds.

I confirmed that this is not a requests problem by using urllib directly. This also takes 2 seconds per request.

I also tried "Connection" "Close", no change...

Any ideas why the request takes 2 seconds with Python but not with Java and NodeJS ?

My code:

import json
from urllib import request
from datetime import datetime

url = "http://localhost:8080/api"

req = request.Request(url, method="POST")
req.add_header('Content-Type', 'application/json')
req.add_header("Connection", "Close")

myData = {
            "schema": "jsonCommand.org/v1",
            "requestId": 1,
            "api": "admin",
            "apiVersion": "1.0",
            "action": "pingSession"
        }

data = json.dumps(myData)
data = data.encode()

for i in range(0, 10):
    now = datetime.now()
    print('Current DateTime:', now)
    with request.urlopen(req, data=data) as response:
        body = response.read()
        print(body)
1

There are 1 answers

0
valenok On

Most probably you're not using mg_http_reply() API. Solution: use it. Reason: your server does not set content length, and/or end-of-response marker. See https://mongoose.ws/tutorials/http-server/#set-content-length details.