本文整理汇总了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
示例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
示例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()
示例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()
示例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
示例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
示例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
示例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