在对ArcGIS API的GET请求期间OpenSSL.SSL.SysCallError:(10054,'WSAECONNRESET')

时间:2018-09-25 14:59:39

标签: python python-requests

版本

Python                3.7.0

pyOpenSSL             18.0.0
requests              2.19.1
requests-futures      0.9.7
urllib3               1.23

https://hazards.fema.gov/gis/nfhl/rest/services/public/NFHL/MapServer/identify

使用Python时,我无法从ArcGIS rest API收到完整响应。我可以通过Chrome浏览到该URL,也可以在Postman中发出GET请求,而不会出现问题。上面的链接是一个成功的GET请求示例,该请求带有我期望的JSON响应。我尝试使用请求,urllib和套接字编写请求。显然,请求和套接字都在recv_into都失败。运行以下代码时,在收到WSAECONNRESET错误之前,我恰好收到了两个块。如果我将几何设置为false发出请求,则响应会小得多,并且返回时不会出现问题。我很茫然,为什么连接仅在Python中关闭,并且确实可以使用一些帮助修复它的方法或进行查询的替代方法。

谢谢!

代码

def FEMA_identify(self, latitude, longitude, layers, is_envelope=False,
                  envelope_range=None, return_geometry=False):
    envelope = self.getEnvelope(envelope_range, latitude, longitude) if\
        is_envelope else self.getPoint(latitude, longitude)

    extent = envelope if is_envelope else\
        self.getEnvelope(100, latitude, longitude)

    geometry_type =\
        "esriGeometryEnvelope" if is_envelope else "esriGeometryPoint"

    payload = {
        "imageDisplay": "1000,1000",
        "geometrytype": geometry_type,
        "geometry":
            f"{envelope.x_min},{envelope.y_min},{envelope.x_max},{envelope.y_max}",
        "tolerance": f"{0}",
        "mapExtent":
            f"{extent.x_min},{extent.y_min},{extent.x_max},{extent.y_max}",
        "f": "JSON",
        "layers": "visible:{}".format(", ".join(map(str, layers))),
        "returnGeometry": f"{return_geometry}"
    }

    url = 'https://hazards.fema.gov' +\
        '/gis/nfhl/rest/services/public/NFHL/MapServer/identify'

    s = requests.Session()
    r = s.get('https://hazards.fema.gov')
    r = s.get(url, params=payload, stream=True)
    response = b''
    idx = 1
    for chunk in r.iter_content(chunk_size=1024):
        if chunk:
            print(f"Received chunk {idx}")
            response += chunk
            idx += 1
    r.close()

跟踪

Traceback (most recent call last):
  File "C:\VirtualEnvs\tfia\lib\site-packages\urllib3\contrib\pyopenssl.py", line 285, in recv_into
    return self.connection.recv_into(*args, **kwargs)
  File "C:\VirtualEnvs\tfia\lib\site-packages\OpenSSL\SSL.py", line 1814, in recv_into
    self._raise_ssl_error(self._ssl, result)
  File "C:\VirtualEnvs\tfia\lib\site-packages\OpenSSL\SSL.py", line 1631, in _raise_ssl_error
    raise SysCallError(errno, errorcode.get(errno))
OpenSSL.SSL.SysCallError: (10054, 'WSAECONNRESET')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\VirtualEnvs\tfia\lib\site-packages\urllib3\response.py", line 331, in _error_catcher
    yield
  File "C:\VirtualEnvs\tfia\lib\site-packages\urllib3\response.py", line 413, in read
    data = self._fp.read(amt)
  File "C:\Program Files\Python37\Lib\http\client.py", line 447, in read
    n = self.readinto(b)
  File "C:\Program Files\Python37\Lib\http\client.py", line 491, in readinto
    n = self.fp.readinto(b)
  File "C:\Program Files\Python37\Lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "C:\VirtualEnvs\tfia\lib\site-packages\urllib3\contrib\pyopenssl.py", line 290, in recv_into
    raise SocketError(str(e))
OSError: (10054, 'WSAECONNRESET')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\VirtualEnvs\tfia\lib\site-packages\requests\models.py", line 749, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\VirtualEnvs\tfia\lib\site-packages\urllib3\response.py", line 465, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "C:\VirtualEnvs\tfia\lib\site-packages\urllib3\response.py", line 430, in read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "C:\Program Files\Python37\Lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\VirtualEnvs\tfia\lib\site-packages\urllib3\response.py", line 349, in _error_catcher
    raise ProtocolError('Connection broken: %r' % e, e)
urllib3.exceptions.ProtocolError: ('Connection broken: OSError("(10054, \'WSAECONNRESET\')")', OSError("(10054, 'WSAECONNRESET')"))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\VirtualEnvs\tfia\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\VirtualEnvs\tfia\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\VirtualEnvs\tfia\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\VirtualEnvs\tfia\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\VirtualEnvs\tfia\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\VirtualEnvs\tfia\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\rallison\Documents\GitHub\PROCESSING-AUTO-UNDERWRITING-LOGIC\auto_underwriting\receiving.py", line 19, in applications
    processApplication(payload)
  File "C:\VirtualEnvs\tfia\lib\site-packages\celery\local.py", line 191, in __call__
    return self._get_current_object()(*a, **kw)
  File "C:\VirtualEnvs\tfia\lib\site-packages\celery\app\task.py", line 375, in __call__
    return self.run(*args, **kwargs)
  File "C:\Users\rallison\Documents\GitHub\PROCESSING-AUTO-UNDERWRITING-LOGIC\auto_underwriting\processing.py", line 13, in processApplication
    processed = Underwriter(coordinates["lat"], coordinates["lng"])
  File "C:\Users\rallison\Documents\GitHub\PROCESSING-AUTO-UNDERWRITING-LOGIC\auto_underwriting\underwriting.py", line 33, in __init__
    self.underwrite(latitude, longitude)
  File "C:\Users\rallison\Documents\GitHub\PROCESSING-AUTO-UNDERWRITING-LOGIC\auto_underwriting\underwriting.py", line 111, in underwrite
    self.search_range, True)["results"]
  File "C:\Users\rallison\Documents\GitHub\PROCESSING-AUTO-UNDERWRITING-LOGIC\auto_underwriting\underwriting.py", line 96, in FEMA_identify
    r = s.get(url, params=payload, stream=False, timeout=30)
  File "C:\VirtualEnvs\tfia\lib\site-packages\requests\sessions.py", line 525, in get
    return self.request('GET', url, **kwargs)
  File "C:\VirtualEnvs\tfia\lib\site-packages\requests\sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\VirtualEnvs\tfia\lib\site-packages\requests\sessions.py", line 662, in send
    r.content
  File "C:\VirtualEnvs\tfia\lib\site-packages\requests\models.py", line 827, in content
    self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
  File "C:\VirtualEnvs\tfia\lib\site-packages\requests\models.py", line 752, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ('Connection broken: OSError("(10054, \'WSAECONNRESET\')")', OSError("(10054, 'WSAECONNRESET')"))

1 个答案:

答案 0 :(得分:0)

我在使用内部服务器时遇到了类似的问题。我通过将openssl从1.1.1降级到1.0.2来解决它。

或者,更简单的解决方法是不使用OpenSSL。导入请求后,立即调用module #( parameter XLEN = 64, parameter USEIP = 0 ) ( input clk, input rstn, input [XLEN-1:0] opA, input [XLEN-1:0] opB, input [XLEN-1:0] opR, input en ); generate case(USEIP) 0:begin always @(posedge clk or negedge rstn) begin if(!rstn) begin opR <= '{default:0}; end else begin if(en) opR <= opA+opB; else opR <= '{default:0}; end end end 1:begin superAdder #(.XLEN(XLEN)) _adder(.clk(clk),.rstm(rstn), .opA(opA), .opB(opB), .opR(opR), .en(en)); end endcase endmodule ,然后将使用python的stdlib SSL实现代替OpenSSL。

这绝对是一个错误,但是我不确定它是openssl,pyopenssl还是错误的服务器配置。