启动服务器和客户端
cd ~/thrift-0.8.0/thrift -r --gen py tutorial.thrift #生成代码cd py./PythonServer.py #启动服务器cd ~/thrift-0.8.0/py./PythonClient.py #在另一个shell窗口启动客户端
结果启动服务器的时候产生一个错误
shujunli@mysvr1:~/thrift-0.8.0/tutorial/py$ ./PythonServer.py Starting the server...Traceback (most recent call last): File "./PythonServer.py", line 95, inserver.serve() File "/usr/lib/python2.7/site-packages/thrift/server/TServer.py", line 74, in serve self.serverTransport.listen() File "/usr/lib/python2.7/site-packages/thrift/transport/TSocket.py", line 136, in listen res0 = self._resolveAddr() File "/usr/lib/python2.7/site-packages/thrift/transport/TSocket.py", line 31, in _resolveAddr return socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG)TypeError: getaddrinfo() argument 1 must be string or None
我们看一下File “PythonServer.py”, line 95,是这句
transport = TSocket.TServerSocket(9090)
我们看TserverSocket源码
def __init__(self, host=None, port=9090, unix_socket=None):
这是因为其初始化函数,第一个参数是host,第二个参数是port,所以想省去host,只填port是不行的,将File “PythonServer.py”, line 95改为:
transport = TSocket.TServerSocket('localhost',9090)
或者
transport = TSocket.TServerSocket(port=9090)
即可解决该问题。