python网络编程-一些常用有用的函数

socket.getservbyname(servicename[, protocolname]) –> integer

查询某个协议对应的端口号,需要使用两个参数,servicename对应端口名称,如 http, smtp,等。protocolname对应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).

 

Socket的异常

  • 与一般I/O和通信问题有关的 socket.error
  • 与查询地址信息有关的 socket.gaierror
  • 与其他地址错误有关的 socket.herror
  • 与在一个socket上调用settimeout()后,处理超时有关的socket.timeout

使用connect()的调用的时候,程序可以解决把主机名转换成IP地址的问题,若主机名不对会产生socket.gaierror,若连接远程主机有问题,会产生socket.error。

posted @ 2013-02-26 10:55  roicel  阅读(530)  评论(0编辑  收藏  举报