我在 python 中使用 xmlrpc 撰寫了這個服務器,我希望能夠從任何計算機訪問這個服務器,但它會引發錯誤。另一件事是如何確保我的服務器一次可以支持多個客戶端?
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(('82.155.18.86', 8000),
requestHandler=RequestHandler) as server:
server.register_introspection_functions()
# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)
# Register a function under a different name
def adder_function(x, y):
return x y
server.register_function(adder_function, 'add')
# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'mul').
class MyFuncs:
def mul(self, x, y):
return x * y
server.register_instance(MyFuncs())
# Run the server's main loop
server.serve_forever()
這是我想在另一臺計算機上運行的客戶端
import xmlrpc.client
s = xmlrpc.client.ServerProxy('http://82.155.18.86:8000')
print(s.pow(2,3)) # Returns 2**3 = 8
print(s.add(2,3)) # Returns 5
print(s.mul(5,2)) # Returns 5*2 = 10
# Print list of available methods
print(s.system.listMethods())
但這就是我得到的回應
C:\Users\Nima\PycharmProjects\RPC\venv\Scripts\python.exe C:/Users/Nima/PycharmProjects/RPC/main.py
Traceback (most recent call last):
File "C:\Users\Nima\PycharmProjects\RPC\main.py", line 4, in <module>
print(s.pow(2,3)) # Returns 2**3 = 8
File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\xmlrpc\client.py", line 1116, in __call__
return self.__send(self.__name, args)
File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\xmlrpc\client.py", line 1456, in __request
response = self.__transport.request(
File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\xmlrpc\client.py", line 1160, in request
return self.single_request(host, handler, request_body, verbose)
File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\xmlrpc\client.py", line 1172, in single_request
http_conn = self.send_request(host, handler, request_body, verbose)
File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\xmlrpc\client.py", line 1285, in send_request
self.send_content(connection, request_body)
File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\xmlrpc\client.py", line 1315, in send_content
connection.endheaders(request_body)
File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1250, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1010, in _send_output
self.send(msg)
File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 950, in send
self.connect()
File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 921, in connect
self.sock = self._create_connection(
File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\socket.py", line 843, in create_connection
raise err
File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\socket.py", line 831, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Process finished with exit code 1
uj5u.com熱心網友回復:
看看檔案。
看來您混淆了埠。服務器埠是8000,但客戶端嘗試連接埠3000。那將不匹配。
使用with陳述句也是一種很好的做法,如檔案中所示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/531623.html
