requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection ')) in python request.api

import requests

resp = requests.api.post(url,headers=self.headers,data=json.dumps(data))

when running it in jenkins, it is working fine sometimes, but in others it is throwing below error

requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

Upvotes: 0

Views: 4406

Answers (1)

J_H
J_H

Reputation: 20425

Your .post() call is correct.

The server is broken and/or your data is not well formed so it causes the server to exhibit broken behavior. (I would expect a 400 or 500 from the server.)

Verify that TCP/IP connectivity is good, e.g. with $ telnet host 443.

Verify that data is of sane length -- sending gigabytes wouldn't make you happy.

EDIT

Now you're saying it is deterministic behavior: 1st works, 2nd fails. This suggests that connection caching is relevant. The requests module maintains a pool of open TCP connections to webservers, in case another request arrives soon.

Perhaps you POSTed, server said 201 Created or whatever, and also said that client should close the connection upon reading status. But client didn't close connection, and is surprised to find the server end is closed when attempting 2nd POST over same connection.

You are running:

resp = requests.api.post(url, ...)

You could instead use:

sess = requests.Session()
resp = sess.post(url, ...)

Then you'd have control over when to discard the session connection pool and make a new one.

Upvotes: 1

Related Questions