我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用_socket.SOCK_DGRAM。
def _udp_socket(address, backlog=50, reuse_addr=None, family=_socket.AF_INET): # backlog argument for compat with tcp_listener # pylint:disable=unused-argument # we want gevent.socket.socket here sock = socket(family=family, type=_socket.SOCK_DGRAM) if reuse_addr is not None: sock.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, reuse_addr) try: sock.bind(address) except _socket.error as ex: strerror = getattr(ex, 'strerror', None) if strerror is not None: ex.strerror = strerror + ': ' + repr(address) raise return sock
def _broadcast_ip(): cs = socket(AF_INET, SOCK_DGRAM) cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) try: while _broadcast: try: cs.sendto("8443".encode("utf-8"), ('255.255.255.255', 42945)) except OSError as network_error: logger.warning("Error while broadcasting: %s", str(network_error)) time.sleep(3) finally: cs.close()
def get_local_ip_address(): s = socket(AF_INET, SOCK_DGRAM) s.connect(("8.8.8.8", 80)) return s.getsockname()[0]
def _udp_socket(address, backlog=50, reuse_addr=None, family=_socket.AF_INET): # we want gevent.socket.socket here sock = socket(family=family, type=_socket.SOCK_DGRAM) if reuse_addr is not None: sock.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, reuse_addr) try: sock.bind(address) except _socket.error: ex = sys.exc_info()[1] strerror = getattr(ex, 'strerror', None) if strerror is not None: ex.strerror = strerror + ': ' + repr(address) raise return sock
def _udp_socket(address, backlog=50, reuse_addr=None, family=_socket.AF_INET): # we want gevent.socket.socket here sock = socket(family=family, type=_socket.SOCK_DGRAM) if reuse_addr is not None: sock.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, reuse_addr) try: sock.bind(address) except _socket.error as ex: strerror = getattr(ex, 'strerror', None) if strerror is not None: ex.strerror = strerror + ': ' + repr(address) raise return sock