简体   繁体   中英

Connection aborted., error(104, 'Connection reset by peer') in Django

I have created an API in Django and inside that calling a third party API which accepts XML data. With 30 rows of XML it is working fine, for more rows throwing the error "Connection aborted.', error(104, 'Connection reset by peer')". The third party also provided a UI so I can test that up to 5000 rows they are accepting and returning proper result. The connection is not getting closed from either of the APIs. What issue can it be?

Expected Result

The post request should work properly for more data in the request as it is working fine for fewer data.

Actual Result

ConnectionError at /v1/send-sms/
('Connection aborted.', error(104, 'Connection reset by peer'))
Request Method: POST
Request URL:    http://example.com/v1/send-sms/
Django Version: 1.11
Exception Type: ConnectionError
Exception Value:    
('Connection aborted.', error(104, 'Connection reset by peer'))
Exception Location: /home/user-135/sandbox/venv/local/lib/python2.7/site-packages/requests/adapters.py in send, line 490
Python Executable:  /home/user-135/sandbox/venv/bin/python
Python Version: 2.7.12
Python Path:    
['/home/user-135/public_html/MyProject',
 '/home/user-135/sandbox/venv/lib/python2.7',
 '/home/user-135/sandbox/venv/lib/python2.7/plat-x86_64-linux-gnu',
 '/home/user-135/sandbox/venv/lib/python2.7/lib-tk',
 '/home/user-135/sandbox/venv/lib/python2.7/lib-old',
 '/home/user-135/sandbox/venv/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/user-135/sandbox/venv/local/lib/python2.7/site-packages',
 '/home/user-135/public_html/MyProject',
 '/home/user-135/public_html/MyProject']
Server time:    Wed, 7 Mar 2018 10:55:19 +0530

Reproduction Steps

import requests

recievers = ''
url = 'https://example.com/sms/'
for x in xrange(0, len(users)):
    y = x + 1
    body_content = body
    phone = mobile_no
    recievers = recievers + "<SMS>.......</SMS>"

xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<!DOCTYPE MESSAGE SYSTEM \"http://127.0.0.1:80/sms/ttr/sms.ptp\">\n<MESSAGE VER=\"1.2\">\n<USER USERNAME=\""+username+"\" PASSWORD=\""+password+"\" />"+recievers+"\n</MESSAGE>"

querystring = {"data": xml,"action":"send"}
headers = {'content-type': "application/xml"}
response = requests.request("POST", url, headers=headers, params=querystring)

Try replacing "params=querystring" with "data=querystring"

 response = requests.request("POST", url, headers=headers, data=querystring)

params seems to be for GET requests and may by your request URL becomes too long after 30 lines of XML.

From docs at : http://docs.python-requests.org/en/master/user/quickstart/

I had a similar case. This error was given by the console when I accessed the delete method via the my API. The problem was solved by fixing the delete function in the view.py file, namely the status argument of Response function that this method returned

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM