本文整理汇总了Python中SocketServer.ThreadingTCPServer.server_bind方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadingTCPServer.server_bind方法的具体用法?Python ThreadingTCPServer.server_bind怎么用?Python ThreadingTCPServer.server_bind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SocketServer.ThreadingTCPServer
的用法示例。
在下文中一共展示了ThreadingTCPServer.server_bind方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: createAndStartServer
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import server_bind [as 别名]
def createAndStartServer():
global serv
ThreadingTCPServer.allow_reuse_address = True
serv = ThreadingTCPServer(('', 20000), SocketHandler,
bind_and_activate=False)
l_onoff = 1
l_linger = 0
serv.socket.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
struct.pack('ii', l_onoff, l_linger))
serv.server_bind()
serv.server_activate()
HP.logger.info('Starting server')
serv.serve_forever() # blocking method
示例2: ConnWebServer
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import server_bind [as 别名]
class ConnWebServer(Thread):
def __init__(self):
Thread.__init__(self)
self.WSPORT = 1240
self.init = False
self.startrecv = False
global g_p
g_p = ConnProc()
global g_connlist
g_connlist = []
global g_connid
g_connid = []
global g_conntype
g_conntype = []
def run(self):
self.initSock()
def executeCommand(self, cmd):
# not implemented yet
#print
r = g_p.ProcCommand(cmd)
print r
def initSock(self):
try:
self.clientWS = ThreadingTCPServer( ('', self.WSPORT), WebSocketServer, False)
print 'listening on PORT(WS)', self.WSPORT
self.clientWS.allow_reuse_address = True
self.clientWS.server_bind()
self.clientWS.server_activate()
self.clientWS.serve_forever()
except Exception, e:
print e
self.init = True;
示例3: server_bind
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import server_bind [as 别名]
def server_bind(self):
# overrides server_bind to store the server name.
ThreadingTCPServer.server_bind(self)
host, port = self.socket.getsockname()[:2]
self.server_name = getfqdn(host)
self.server_port = port
示例4: PySAPNIStreamSocketTest
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import server_bind [as 别名]
class PySAPNIStreamSocketTest(unittest.TestCase):
test_port = 8005
test_address = "127.0.0.1"
test_string = "TEST" * 10
def start_server(self, handler_cls):
self.server = ThreadingTCPServer((self.test_address, self.test_port),
handler_cls,
bind_and_activate=False)
self.server.allow_reuse_address = True
self.server.server_bind()
self.server.server_activate()
self.server_thread = Thread(target=self.server.serve_forever)
self.server_thread.start()
def stop_server(self):
self.server.shutdown()
self.server.server_close()
self.server_thread.join()
def test_sapnistreamsocket(self):
"""Test SAPNIStreamSocket"""
self.start_server(SAPNITestHandler)
sock = socket.socket()
sock.connect((self.test_address, self.test_port))
self.client = SAPNIStreamSocket(sock)
packet = self.client.sr(self.test_string)
packet.decode_payload_as(Raw)
self.client.close()
self.assertIn(SAPNI, packet)
self.assertEqual(packet[SAPNI].length, len(self.test_string))
self.assertEqual(packet.payload.load, self.test_string)
self.stop_server()
def test_sapnistreamsocket_base_cls(self):
"""Test SAPNIStreamSocket handling of custom base packet classes"""
self.start_server(SAPNITestHandler)
class SomeClass(Packet):
fields_desc = [StrField("text", None)]
sock = socket.socket()
sock.connect((self.test_address, self.test_port))
self.client = SAPNIStreamSocket(sock, base_cls=SomeClass)
packet = self.client.sr(self.test_string)
self.client.close()
self.assertIn(SAPNI, packet)
self.assertIn(SomeClass, packet)
self.assertEqual(packet[SAPNI].length, len(self.test_string))
self.assertEqual(packet[SomeClass].text, self.test_string)
self.stop_server()
def test_sapnistreamsocket_getnisocket(self):
"""Test SAPNIStreamSocket get nisocket class method"""
self.start_server(SAPNITestHandler)
self.client = SAPNIStreamSocket.get_nisocket(self.test_address,
self.test_port)
packet = self.client.sr(self.test_string)
packet.decode_payload_as(Raw)
self.client.close()
self.assertIn(SAPNI, packet)
self.assertEqual(packet[SAPNI].length, len(self.test_string))
self.assertEqual(packet.payload.load, self.test_string)
self.stop_server()
def test_sapnistreamsocket_without_keep_alive(self):
"""Test SAPNIStreamSocket without keep alive"""
self.start_server(SAPNITestHandlerKeepAlive)
sock = socket.socket()
sock.connect((self.test_address, self.test_port))
self.client = SAPNIStreamSocket(sock, keep_alive=False)
packet = self.client.sr(self.test_string)
packet.decode_payload_as(Raw)
self.client.close()
# We should receive a PING instead of our packet
self.assertIn(SAPNI, packet)
self.assertEqual(packet[SAPNI].length, len(SAPNI.SAPNI_PING))
self.assertEqual(packet.payload.load, SAPNI.SAPNI_PING)
self.stop_server()
def test_sapnistreamsocket_with_keep_alive(self):
"""Test SAPNIStreamSocket with keep alive"""
self.start_server(SAPNITestHandlerKeepAlive)
#.........这里部分代码省略.........
示例5: server_bind
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import server_bind [as 别名]
def server_bind(self):
"""
override base class to use REUSEADDR option
"""
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
ThreadingTCPServer.server_bind(self)
示例6: create_server
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import server_bind [as 别名]
def create_server(addr, port, handler):
server = ThreadingTCPServer((addr, port), handler, bind_and_activate=False)
server.allow_reuse_address = True
server.server_bind()
server.server_activate()
server.serve_forever()
示例7: from
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import server_bind [as 别名]
#self.wfile.write(data.upper())
# #转换成大写后写回(发生到)客户端
# #self.request.sendall(data.upper())
except EOFError:
print "cut from (%r):%r,offline" % (self.client_address, data)
del client_list[str(self.client_address[1])]
del client_socket_list[str(self.client_address[1])]
break
except socket.error, e:
print "x(%r):%r,offline" % (self.client_address, data)
del client_list[str(self.client_address[1])]
del client_socket_list[str(self.client_address[1])]
break
except:
print "z(%r) offline" % (self.client_address,)
del client_list[str(self.client_address[1])]
del client_socket_list[str(self.client_address[1])]
break
if __name__ == "__main__":
addr = (__localhost__, __testPort__)
#购置TCPServer对象,
server = ThreadingTCPServer(addr, baseRequestHandlerr,bind_and_activate=False)
server.allow_reuse_address = True
server.server_bind()
server.server_activate()
#启动服务监听
print 'start,port 27017'
server.serve_forever()