python accept函数_Python socket 模块,accept() 实例源码 - 编程字典

def tick(self):

"""Accept a new connection and put it on the Queue."""

try:

s, addr = self.socket.accept()

prevent_socket_inheritance(s)

if not self.ready:

return

if hasattr(s, 'settimeout'):

s.settimeout(self.timeout)

environ = self.environ.copy()

# SERVER_SOFTWARE is common for IIS. It's also helpful for

# us to pass a default value for the "Server" response header.

if environ.get("SERVER_SOFTWARE") is None:

environ["SERVER_SOFTWARE"] = "%sWSGI Server" % self.version

# set a non-standard environ entry so the WSGI app can know what

# the *real* server protocol is (and what features to support).

# See http://www.faqs.org/rfcs/rfc2145.html.

environ["ACTUAL_SERVER_PROTOCOL"] = self.protocol

environ["SERVER_NAME"] = self.server_name

if isinstance(self.bind_addr, basestring):

# AF_UNIX. This isn't really allowed by WSGI, which doesn't

# address unix domain sockets. But it's better than nothing.

environ["SERVER_PORT"] = ""

else:

environ["SERVER_PORT"] = str(self.bind_addr[1])

# optional values

# Until we do DNS lookups, omit REMOTE_HOST

environ["REMOTE_ADDR"] = addr[0]

environ["REMOTE_PORT"] = str(addr[1])

conn = self.ConnectionClass(s, self.wsgi_app, environ)

self.requests.put(conn)

except socket.timeout:

# The only reason for the timeout in start() is so we can

# notice keyboard interrupts on Win32, which don't interrupt

# accept() by default

return

except socket.error, x:

if x.args[0] in socket_error_eintr:

# I *think* this is right. EINTR should occur when a signal

# is received during the accept() call; all docs say retry

# the call, and I *think* I'm reading it right that Python

# will then go ahead and poll for and handle the signal

# elsewhere. See http://www.cherrypy.org/ticket/707.

return

if x.args[0] in socket_errors_nonblocking:

# Just try again. See http://www.cherrypy.org/ticket/479.

return

if x.args[0] in socket_errors_to_ignore:

# Our socket was closed.

# See http://www.cherrypy.org/ticket/686.

return

raise

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值