当前位置: 首页>>代码示例>>Python>>正文


Python socket.setsockopt方法代码示例

本文整理汇总了Python中socket.socket.setsockopt方法的典型用法代码示例。如果您正苦于以下问题:Python socket.setsockopt方法的具体用法?Python socket.setsockopt怎么用?Python socket.setsockopt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在socket.socket的用法示例。


在下文中一共展示了socket.setsockopt方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: create_broadcast_sockets

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import setsockopt [as 别名]
def create_broadcast_sockets():
    sender = Socket(AF_INET, SOCK_DGRAM)
    sender.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
    sender.setsockopt(SOL_SOCKET, SO_BROADCAST, True)
    sender.bind(("", 0))
    receiver = Socket(AF_INET, SOCK_DGRAM)
    receiver.settimeout(constants.broadcast_receiver_timeout)
    receiver.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
    receiver.setsockopt(SOL_SOCKET, SO_BROADCAST, True)
    receiver.bind(("", constants.broadcast_port))
    return sender, receiver
开发者ID:giflw,项目名称:afn-tools,代码行数:13,代码来源:discovery.py

示例2: run_server

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import setsockopt [as 别名]
def run_server(server_port):
    """Run the UDP pinger server
    """

    # Create the server socket (to handle UDP requests using ipv4), make sure
    # it is always closed by using with statement.
    server_socket = Socket(socket.AF_INET, socket.SOCK_DGRAM)

    # The socket stays connected even after this script ends. So in order
    # to allow the immediate reuse of the socket (so that we can kill and
    # re-run the server while debugging) we set the following option. This
    # is potentially dangerous in real code: in rare cases you may get junk
    # data arriving at the socket.
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    # Set the server port
    server_socket.bind(("", server_port))

    # Start accepting ping requests
    print ("Ping server ready on port", server_port)
    while True:
        # Receive message and send one back
        _, client_address = server_socket.recvfrom(1024)
        server_socket.sendto("Pong".encode(), client_address)

    return 0
开发者ID:rodrigoncalves,项目名称:myhttpd,代码行数:28,代码来源:ping.py

示例3: main

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import setsockopt [as 别名]
def main():
	parser = argparse.ArgumentParser()
	parser.add_argument('--port', '-p', default=8080, type=int,
	                    help='Port to use')
	args = parser.parse_args()

	try:
	    server_socket = Socket(socket.AF_INET, socket.SOCK_STREAM)
	    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
	    # server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, BUFFER_SIZE)

	    server_socket.bind(('', args.port))
	    server_socket.listen(1)

	    cache_dict = {}

	    print "Proxy server ready..."

	    while True:
	    	try:
	    		connection_socket = server_socket.accept()[0]

	    		t = Thread(target=handle_http, args=[cache_dict, connection_socket])
	    		t.setDaemon(1)
	    		t.start()
	    		t.join()
	    	except socket.error, e:
	    		print e
	    	finally:
	    		connection_socket.close()
开发者ID:shenaishiren,项目名称:computer-network-a-top-approach,代码行数:32,代码来源:web-proxy.py

示例4: main

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import setsockopt [as 别名]
def main():
    register_builtin_interface()
    server = Socket()
    if len(sys.argv) > 1:
        server_port = int(sys.argv[1])
    else:
        server_port = DEFAULT_PORT
    print "Listening on port " + str(server_port)
    server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    server.bind(("", server_port))
    server.listen(50)
    
    Thread(target=process_event_queue).start()
    
    print "\nAutobus has successfully started up."
    
    try:
        while True:
            socket, address = server.accept()
            connection = Connection(socket, address)
            event_queue.put((connection.id, discard_args(connection.register)), block=True)
            connection.start()
    except KeyboardInterrupt:
        print "KeyboardInterrupt received, shutting down"
        event_queue.put((None, None), block=True)
        print "Event queue has been notified to shut down"
    except:
        print "Unexpected exception occurred in the main loop, shutting down. Stack trace:"
        print_exc()
        event_queue.put((None, None), block=True)
        print "Event queue has been notified to shut down"
    server.close()
开发者ID:giflw,项目名称:afn-tools,代码行数:34,代码来源:__init__.py

示例5: bind_reuse

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import setsockopt [as 别名]
def bind_reuse(ip, port):
    sock = Socket()
    sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    sock.bind((ip, port))
    return sock
开发者ID:jolynch,项目名称:pip-faster,代码行数:7,代码来源:ephemeral_port_test.py

示例6: listen

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import setsockopt [as 别名]
 def listen(self):
     """
     Bind and listen.
     :return: The open socket.
     :rtype: socket.socket
     """
     address = (self.host, self.port)
     socket = Socket(AF_INET, SOCK_STREAM)
     socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
     socket.bind(address)
     socket.listen(5)
     log.info('listening on: %d', self.port)
     return socket
开发者ID:darinlively,项目名称:gofer,代码行数:15,代码来源:manager.py

示例7: run_server

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import setsockopt [as 别名]
def run_server(server_port):
	# Run UDP pinger server

	try:
		server_socket = Socket(socket.AF_INET, socket.SOCK_DGRAM)
		server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

		server_socket.bind(('', server_port))

		print("Ping server ready on port", server_port)
		while True:
			_, client_address = server_socket.recvfrom(1024)
			server_socket.sendto("".encode(), client_address)
	finally:
		server_socket.close()

	return 0
开发者ID:shenaishiren,项目名称:computer-network-a-top-approach,代码行数:19,代码来源:ping.py

示例8: main

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import setsockopt [as 别名]
def main():
	parser=argparse.ArgumentParser()
	parser.add_argument('--port','-p',default=2000,type=int,help='port to use')
	args=parser.parse_args()
	server_socket=Socket(socket.AF_INET,socket.SOCK_STREAM)
	server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
	server_socket.bind(('',args.port))
	server_socket.listen(1)
	print "server running"
	while True:
		connection_socket=server_socket.accept()[0]
		request=connection_socket.recv(1024)
		reply=http_handler(request)
		connection_socket.send("HTTP/1.1 200 OK\n") 
		connection_socket.send("\n")

		connection_socket.send(reply)
		connection_socket.close()

		print "received request"
		print "reply sent"

	return 0
开发者ID:p1p13,项目名称:Computer-Networking-A-Top-Down-Approach,代码行数:25,代码来源:webserver.py


注:本文中的socket.socket.setsockopt方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。