从命名元组问题创建套接字

时间:2016-10-24 08:59:25

标签: python sockets

从返回值os socket.getaddrinfo创建一个python服务器套接字,它返回一个5元组序列(family, socktype, proto, canonname, sockaddr)
这是我的代码。

 import socket
 import collections

sock_info_tuple = namedtuple('sock_info_tuple', 'family type proto  cannon sockaddr_tuple')
socket_info =   sock_info_tuple._make(socket.getaddrinfo('127.0.0.1',80,socket.AF_INET,socket.SOCK_STREAM,socket.IPPROTO_TCP))

TypeError                                 Traceback (most recent call last)
<ipython-input-90-b04233a8e38d> in <module>()
  5 #get the socket info
  6 sock_info_tuple = namedtuple('sock_info_tuple', 'family type proto cannon sockaddr_tuple')
----> 7 socket_info =  sock_info_tuple._make(socket.getaddrinfo('127.0.0.1',80,socket.AF_INET,socket     .SOCK_STREAM,socket.IPPROTO_TCP))
  8 
  9 #create the server scket and pass the arguments

<string> in _make(cls, iterable, new, len)

TypeError: Expected 5 arguments, got 1   

所以可能是问题,但socket.getaddrinfo返回元组

1 个答案:

答案 0 :(得分:1)

如果 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0 0 0 0 0 0 0 0 0 0 [2,] 0 0 0 0 0 0 0 0 0 0 [3,] 0 0 0 0 0 0 0 0 0 0 [4,] 1 1 1 1 0 0 0 0 0 0 [5,] 1 1 1 1 0 0 0 0 0 0 [6,] 1 1 1 1 0 0 0 0 0 0 [7,] 1 1 1 1 0 0 0 0 0 0 [8,] 1 1 1 1 0 0 0 0 0 0 [9,] 1 1 1 1 0 0 0 0 0 0 [10,] 1 1 1 1 0 0 0 0 0 0 socket.getaddrinfo作为元组返回,则应使用(family, socktype, proto, canonname, sockaddr)解包此元组。

*