啟動伺服器和用戶端
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, in <module> server.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)
即可解決該問題。