Python XMLRPC SSL客户端和服务器

时间:2019-03-14 05:02:37

标签: python ssl server client rpc

我正在尝试使用SSL设置简单的XMLRPC服务器和客户端。从Python3 documentation开始,我从服务器端代码开始以下内容:

from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
with SimpleXMLRPCServer(('localhost', 8000),
                        requestHandler=RequestHandler) as server:

    # Register pow() function; this will use the value of
    # pow.__name__ as the name, which is just 'pow'.
    server.register_function(pow)

    # Run the server's main loop
    server.serve_forever()

从客户端开始,我从:

import xmlrpc.client

s = xmlrpc.client.ServerProxy('http://localhost:8000')
print(s.pow(2,3))  # Returns 2**3 = 8

以上方法有效,但现在我正在尝试通过SSL进行操作。我用以下方法创建了一个cert.pem文件:

openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem

并如下修改服务器代码:

import ssl
from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
with SimpleXMLRPCServer(('localhost', 8000),
                        requestHandler=RequestHandler) as server:
    server.socket = ssl.wrap_socket(server.socket, server_side=True, certfile='cert.pem', cert_reqs=ssl.CERT_NONE)

    # Register pow() function; this will use the value of
    # pow.__name__ as the name, which is just 'pow'.
    server.register_function(pow)

    # Run the server's main loop
    server.serve_forever()

服务器似乎启动正常,并且如果我尝试通过未修改的客户端连接到该服务器,则该连接未按预期建立,因为该客户端未使用SSL。因此,然后我尝试通过以下修改将SSL添加到客户端:

import ssl
import xmlrpc.client

context = ssl.SSLContext()
s = xmlrpc.client.ServerProxy('http://localhost:8000', context=context)
print(s.pow(2,3))  # Returns 2**3 = 8

但是即使那样,连接仍然失败:

$ python3 xmlClient.py
Traceback (most recent call last):
  File "xmlClient.py", line 6, in <module>
    print(s.pow(2,3))  # Returns 2**3 = 8
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1112, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1452, in __request
    verbose=self.__verbose
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1154, in request
    return self.single_request(host, handler, request_body, verbose)
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1167, in single_request
    resp = http_conn.getresponse()
  File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/usr/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.6/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer

我不确定我是否正确设置了服务器或客户端。非常感谢您对确定问题的任何帮助。

0 个答案:

没有答案