Maison  >  Article  >  développement back-end  >  python网络编程中常用到的函数​总结

python网络编程中常用到的函数​总结

高洛峰
高洛峰original
2016-10-20 10:24:031497parcourir

总结一下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。


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:Python集合(set)类型的操作总结Article suivant:python多线程