Think before you speak, read before you think.

python的socket中的getservbyname()函数

getservbyname()函数

可以根据端口名,如tcp,udp 和端口名,如http 。找到这个端口的端口号。

如下程序

#!/usr/bin/env python

import socket
port = socket.getservbyname('http','tcp')
print "http's port is %d" % port

运行后会显示

http’s port is 80

参见:http://docs.python.org/library/socket.html

:)EOT


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *