0

I want to use the Python lib requests to connect to my ASP Net Core API point.

After trying to send a Json from Python to ASP Net core I get the following message:

ConnectionError: HTTPConnectionPool(host='localhost', port=5000): Max retries exceeded with url: /api/Search (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f66d3773880>: Failed to establish a new connection: [Errno 111] Connection refused'))

btw: I have only tried once to establish the connection ;-)

import requests
import json

class SearchApi:

@staticmethod
 def findFile(request):
    
    headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
    url     = "http://localhost:5000/api/Search"
    response = requests.post(url, data=json.dumps(request.__dict__), headers=headers)

    return response.json()

and request class:

class Request:

def __init__(self, driveId, itemId, searchName):
    self.driveId = driveId
    self.itemId = itemId
    self.searchName = searchName

and Asp .Net Core back-End method:

        [ApiController]
        [Route("api/[controller]")]
      public class SearchController : ControllerBase
         [HttpPost]
       public async Task<IActionResult> SearchFile([FromBody] SearchInfos searchInfos)
       {
        //......
        Console.WriteLine("Ping ! ! ");
        return Ok();
    }

With the backend, unfortunately, no request arrives from Python, but the Postman or Swagger tests are successful.

What mistake have I made in the code ?

1 Answer 1

1

Update

The reason for the problem is that the ngrok proxy is used. Use the following command to solve this problem.

ngrok http -host-header=localhost:5000

Try to set keep_alive = False.

s = requests.session()
s.keep_alive = False

Try to add Connection=close in headers.

headers = {
            'Connection': 'close',
            'Content-type': 'application/json', 
            'Accept': 'application/json'
          }
Sign up to request clarification or add additional context in comments.

7 Comments

Hello and thank you for your reply, but unfortunately the error message remains.
@ChsharpNewbie Can you try my updated answer?
have tried, unfortunately also does not work
@ChsharpNewbie Can you tell me, your webapi and python code in same pc ?
@ChsharpNewbie I think it was the firewall block port 5000. You need to allow it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.