连接中止。,Django中的错误(104,'连接由同行重置')

时间:2018-03-07 06:38:30

标签: python django python-2.7 python-requests

我在Django中创建了一个API,调用了一个接受XML数据的第三方API。使用30行XML可以正常工作,因为更多行会引发错误"连接中止。',错误(104,'连接由同行重置')"。第三方还提供了一个UI,因此我可以测试他们接受的最多5000行并返回正确的结果。 任何API都不会关闭连接。 它可能是什么问题?

预期结果

对于请求中的更多数据,post请求应该可以正常工作,因为它可以正常工作以减少数据。

实际结果

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

复制步骤

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)

2 个答案:

答案 0 :(得分:1)

尝试替换&#34; params = querystring&#34;使用&#34; data = querystring&#34;

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

params似乎是针对GET请求的,并且在30行XML之后,您的请求URL可能会变得太长。

来自http://docs.python-requests.org/en/master/user/quickstart/

的文档

答案 1 :(得分:0)

我也遇到过类似的情况。当我通过我的 API 访问 delete 方法时,控制台给出了这个错误。通过修复view.py文件中的delete函数,即该方法返回的Response函数的status参数

解决了问题