PYTHON请求错误:(“连接已中止。”,OSError(“(10054,'WSAECONNRESET')”)))

时间:2019-01-10 09:26:40

标签: python-3.x python-requests

我正在尝试使用肥皂从Web服务获取数据。到目前为止,我已经获得了成功的响应,但是现在我的代码抛出了此错误。关于为什么每天都能正常工作的代码现在引发此错误的任何想法? 请注意,这不是公共API,您的IP必须在白名单中才能检索数据。

编辑代码:

import requests
headers = {'x-ibm-client-id': "MY KEY",
       'content-type': 'application/soap+xml',
       'accept':'application/xml'}

url="https://api.epias.com.tr/epias/exchange/electricity/balancingMarket"
login="""<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
 <soapenv:Header>
  <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
   <wsse:UsernameToken>
    <wsse:Username>string</wsse:Username>
    <wsse:Password>string</wsse:Password>
    <wsse:Nonce EncodingType="string">string</wsse:Nonce>
    <wsu:Created>string</wsu:Created>
   </wsse:UsernameToken>
   <wsu:Timestamp wsu:Id="string">
    <wsu:Created>string</wsu:Created>
    <wsu:Expires>string</wsu:Expires>
   </wsu:Timestamp>
  </wsse:Security>
 </soapenv:Header>
 <soapenv:Body>
  <dgp:login xmlns:dgp="http://ws.dgpys.deloitte.com"><!-- mandatory -->
   <loginMessage>
    <Password v="MYPASSWORD"></Password>
    <UserName v="MYUSERNAME"></UserName>
   </loginMessage>
  </dgp:login>
 </soapenv:Body>
 """
s=requests.Session()

s.get(url)
res=s.post(url,data=login,headers=headers)

1 个答案:

答案 0 :(得分:0)

保留上面的代码,您应该处理代理设置。

import urlparse,urllib2


s=requests.Session()
opener=urllib2.build_opener()
if proxy:

    proxy_params={urlparse.urlparse(url).scheme : proxy}
    opener.add_handler(urllib2.ProxyHandler(proxy_params))
s.get(url)
res=s.post(url,data=login,headers=headers)

如果您的代码曾经可以正常工作,则该站点可能已阻止您的IP或不喜欢您的标头设置。