Heim  >  Artikel  >  Backend-Entwicklung  >  python网络编程中常用到的函数​总结

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

高洛峰
高洛峰Original
2016-10-20 10:24:031498Durchsuche

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


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Python集合(set)类型的操作总结Nächster Artikel:python多线程