注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Code@Pig Home

喜欢背着一袋Code傻笑的Pig .. 忧美.欢笑.记忆.忘却 .之. 角落

 
 
 

日志

 
 

[Python] socket options

2006-09-24 18:03:56|  分类: lang_python |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Python 中的 socket options 对应于 setsockopt()/getsockopt() 两个函数。函数声明如下:
  setsockopt(level, optname, value)
  getsockopt(level, optname[, buflen])

buflen must be specified and gives the maximum length of a string you're willing to accept.

对于 level,用于定义 which set of options is to be used。一般只有 SOL_SOCKET,其它的 level 都是操作系统相关,不具备可移植性。下面是 SOL_SOCKET 下可设置的一些 optname。


Option Meaning Expected Value
SO_BINDTODEVICE Causes the socket to be valid on a particular network interface (network card) only. May not be portable. A string giving the device name, or an empty string to return to default behavior
SO_BROADCAST Permits the transmission and reception of packets to the broadcast address. Only valid for UDP. Boolean integer
SO_DONTROUTE Prohibits outgoing packets from crossing any router or gateway. This is most frequently useful for UDP communication on an Ethernet LAN as a security measure. It prevents data from leaving the local network, no matter what IP address is used for the destination. Boolean integer
SO_KEEPALIVE Enables the transmission of keep-alive packets for TCP connections. These packets help both endpoints of the connection verify that the connection remains open even when no data is passing through it. Boolean integer
SO_OOBINLINE Causes incoming out-of-band data to be treated as if it were normal data; that is, it will be receivable via a standard call to recv(). Boolean integer
SO_REUSEADDR The port number used by the local endpoint of this socket becomes immediately reusable after the socket is closed. Normal behavior prevents it from being reused for a system-defined amount of time. Boolean integer
SO_TYPE Retrieves the socket type (such as SOCKSTREAM or SOCK_DGRAM). getsockopt() only. Integer


下面的代码可以看到我们当前的 Python 支持哪些 optname。

import socket
solist = [x for x in dir(socket) if x.startswith('SO_')]
solist.sort()
for x in solist:
    print x


  评论这张
 
阅读(990)| 评论(3)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017