socket.getservbyname(servicename[, protocolname]) –> integer
查詢某個(gè)協(xié)議對(duì)應(yīng)的端口號(hào),需要使用兩個(gè)參數(shù),servicename對(duì)應(yīng)端口名稱,如 http, smtp,等。protocolname對(duì)應(yīng)tcp,udp。s.getsockname()
s.getpeername()
getsockname: Return the address of the local endpoint. For IP sockets, the address info is a pair (hostaddr, port)
getpeername: Return the address of the remote endpoint. For IP sockets, the address info is a pair (hostaddr, port).
使用connect()的調(diào)用的時(shí)候,程序可以解決把主機(jī)名轉(zhuǎn)換成IP地址的問題,若主機(jī)名不對(duì)會(huì)產(chǎn)生socket.gaierror,若連接遠(yuǎn)程主機(jī)有問題,會(huì)產(chǎn)生socket.error。