本文整理汇总了Python中SocketServer.UDPServer.allow_reuse_address方法的典型用法代码示例。如果您正苦于以下问题:Python UDPServer.allow_reuse_address方法的具体用法?Python UDPServer.allow_reuse_address怎么用?Python UDPServer.allow_reuse_address使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SocketServer.UDPServer
的用法示例。
在下文中一共展示了UDPServer.allow_reuse_address方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test
# 需要导入模块: from SocketServer import UDPServer [as 别名]
# 或者: from SocketServer.UDPServer import allow_reuse_address [as 别名]
def test(self, selftest):
# We need to discover SERVEP_IP and set up SERVER_PORT
# Note: Port 7 is Echo Protocol:
#
# Port number rationale:
#
# The Echo Protocol is a service in the Internet Protocol Suite defined
# in RFC 862. It was originally proposed for testing and measurement
# of round-trip times[citation needed] in IP networks.
#
# A host may connect to a server that supports the Echo Protocol using
# the Transmission Control Protocol (TCP) or the User Datagram Protocol
# (UDP) on the well-known port number 7. The server sends back an
# identical copy of the data it received.
SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
SERVER_PORT = 32767
# Returning none will suppress host test from printing success code
server = UDPServer((SERVER_IP, SERVER_PORT), UDPEchoClient_Handler)
server.allow_reuse_address = True
print "HOST: Listening for UDP connections..."
self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT)
server.serve_forever()
示例2: except
# 需要导入模块: from SocketServer import UDPServer [as 别名]
# 或者: from SocketServer.UDPServer import allow_reuse_address [as 别名]
except (IOError, TypeError, OSError, ValueError, IndexError), err:
print "Error parsing request data: %s" % err
return "ERR"
class UDPHandle(BaseRequestHandler):
def handle(self):
addr = self.client_address
sock = self.request[1]
resp = ""
try:
raw_data = str(self.request[0]).strip().replace('\n', '')
if EOF not in raw_data or SOF not in raw_data:
print "Failed to pull entire stream..."
sock.sendto(ON_FAIL, addr)
return
print "From (%s): %s" % (str(addr), raw_data)
resp = data_handle(raw_data)
sock.sendto(SOF + resp + EOF, addr)
except (OSError, ValueError, TypeError, IOError), err:
print "UDP handle error: %s" % err
sock.sendto(SOF + resp + EOF, addr)
if __name__ == "__main__":
worx.set_thing(worx_thing)
server = UDPServer((BIND_ADDR, BIND_PORT), UDPHandle)
server.allow_reuse_address = True
print "Starting server...\nwaiting for data..."
server.serve_forever()