0

我的配置文件:

Host server
User new_user
HostName 10.0.1.193
Port 55555
LocalForward 3000 10.0.1.193:6000
IdentityFile ~/.ssh/server

客户端.py

import xmlrpclib
s = xmlrpclib.ServerProxy('http://localhost:3000')
print s.pow(2,3)  # Returns 2**3 = 8
print s.add(2,3)  # Returns 5
print s.div(5,2)  # Returns 5//2 = 2

# Print list of available methods
print s.system.listMethods()

服务器.py

from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

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

# Create server
server = SimpleXMLRPCServer(("localhost", 6000),
                            requestHandler=RequestHandler)
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 'div').
class MyFuncs:
    def div(self, x, y):
        return x // y

server.register_instance(MyFuncs())

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

我的 server.py 运行良好,但是当我运行 client.py 时,它给出了以下错误:

Traceback (most recent call last):
  File "client.py", line 4, in <module>
    print s.pow(2,3)  # Returns 2**3 = 8
  File "/usr/lib/python2.7/xmlrpclib.py", line 1224, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1578, in __request
    verbose=self.__verbose
  File "/usr/lib/python2.7/xmlrpclib.py", line 1264, in request
    return self.single_request(host, handler, request_body, verbose)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1292, in single_request
    self.send_content(h, request_body)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1439, in send_content
    connection.endheaders(request_body)
  File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 814, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 776, in send
    self.connect()
  File "/usr/lib/python2.7/httplib.py", line 757, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 111] Connection refused

我已经检查了我的 ssh 是否正常工作,我可以使用给定的配置 ssh 进入远程服务器,即

ssh server

作品找到。谁能解释可能出了什么问题?

4

1 回答 1

0

您的服务器运行,也许它没有抱怨,但这并不意味着它“运行正确”或更明确地说,它并不意味着服务器处于客户端期望的工作状态。

上面的内容有点神秘:一些未知的东西出了问题,即使你还不知道出了什么问题,你想开始测试你知道应该工作的东西,并验证它们实际上是否工作。即使错误对您来说毫无意义,这也是一种有用的调试技巧。

在这种情况下,客户端错误消息是“连接被拒绝”,意思是“拒绝 [在服务器上]”。

试试这个:在终端/DOS 窗口中的“客户端”PC 上,运行:telnet [您的服务器 ip] [您的服务器端口]

您应该会遇到同样的错误 - 连接被拒绝。也许服务器实际上并没有打开端口。或者服务器打开了端口,但由于服务器上的防火墙,您无法在另一台主机上远程看到它。

此外,在同一主机上同时运行客户端和服务器代码有时可以揭示更多线索(它应该可以工作,但如果不能,那么可能存在不止一个问题)。

于 2013-10-16T18:33:27.963 回答