Ssh client.py无法正常工作,显示连接错误

时间:2013-03-17 19:44:34

标签: ssh openssh

我的配置文件:

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

Client.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()

Server.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

找到工作。任何人都可以解释可能出现的问题吗?

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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

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