本文整理汇总了Python中socket.SOCK_DGRAM属性的典型用法代码示例。如果您正苦于以下问题:Python socket.SOCK_DGRAM属性的具体用法?Python socket.SOCK_DGRAM怎么用?Python socket.SOCK_DGRAM使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.SOCK_DGRAM属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def __init__(self, address='0.0.0.0', broadcast='255.255.255.255'):
# Prepare a socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.bind((address, 0))
self._socket = sock
self._listener = ListenerThread(sock, self._handle_packet)
self._listener.start()
self._packet_handlers = {}
self._current_handler_id = 0
self._broadcast = broadcast
示例2: serve
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def serve(self):
server_socket = None
try:
common.internal_print("Starting module: {0} on {1}:{2}".format(self.get_module_name(), self.config.get("Global", "serverbind"), int(self.config.get(self.get_module_configname(), "serverport"))))
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
whereto = (self.config.get("Global", "serverbind"), int(self.config.get(self.get_module_configname(), "serverport")))
server_socket.bind(whereto)
self.comms_socket = server_socket
self.serverorclient = 1
self.authenticated = False
self.communication_initialization()
self.communication(False)
except KeyboardInterrupt:
self.cleanup()
return
self.cleanup()
return
示例3: connect
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def connect(self):
try:
common.internal_print("Starting client: {0}".format(self.get_module_name()))
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.server_tuple = (self.config.get("Global", "remoteserverip"), int(self.config.get(self.get_module_configname(), "serverport")))
self.comms_socket = server_socket
self.serverorclient = 0
self.authenticated = False
self.do_hello()
self.communication(False)
except KeyboardInterrupt:
self.do_logoff()
self.cleanup()
raise
except socket.error:
self.cleanup()
raise
self.cleanup()
return
示例4: send
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def send(cmd):
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.settimeout(1)
flag = False
for i in range(3):
try:
s.sendto(cmd, addr)
while 1:
data, a = s.recvfrom(1024)
if 'ok' in data:
flag = True
break
except socket.timeout:
continue
except:
break
if flag:
break
s.close()
return flag
示例5: _connect_unixsocket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def _connect_unixsocket(self, address):
use_socktype = self.socktype
if use_socktype is None:
use_socktype = socket.SOCK_DGRAM
self.socket = socket.socket(socket.AF_UNIX, use_socktype)
try:
self.socket.connect(address)
# it worked, so set self.socktype to the used type
self.socktype = use_socktype
except socket.error:
self.socket.close()
if self.socktype is not None:
# user didn't specify falling back, so fail
raise
use_socktype = socket.SOCK_STREAM
self.socket = socket.socket(socket.AF_UNIX, use_socktype)
try:
self.socket.connect(address)
# it worked, so set self.socktype to the used type
self.socktype = use_socktype
except socket.error:
self.socket.close()
raise
示例6: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def __init__(self, device, baud=None, input=True, broadcast=False, source_system=255, source_component=0, use_native=mavutil.default_native):
self._logger = logging.getLogger(__name__)
a = device.split(':')
if len(a) != 2:
self._logger.critical("UDP ports must be specified as host:port")
sys.exit(1)
self.port = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.udp_server = input
self.broadcast = False
self.addresses = set()
if input:
self.port.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.port.bind((a[0], int(a[1])))
else:
self.destination_addr = (a[0], int(a[1]))
if broadcast:
self.port.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.broadcast = True
mavutil.set_close_on_exec(self.port.fileno())
self.port.setblocking(False)
mavutil.mavfile.__init__(self, self.port.fileno(), device, source_system=source_system, source_component=source_component, input=input, use_native=use_native)
示例7: _create_ipv4_sockets
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def _create_ipv4_sockets(loopback_enabled):
# Open a multicast send socket, with IP_MULTICAST_LOOP enabled or disabled as requested.
mcast_address = "224.0.1.195"
port = 49501
group = (mcast_address, port)
txsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
txsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if loopback_enabled:
txsock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
else:
txsock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)
txsock.connect(group)
# Open a multicast receive socket and join the group
rxsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
req = struct.pack("=4sl", socket.inet_aton(mcast_address), socket.INADDR_ANY)
rxsock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, req)
rxsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
rxsock.bind(group)
return (txsock, rxsock)
示例8: _create_ipv6_sockets
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def _create_ipv6_sockets(loopback_enabled):
# Open a multicast send socket, with IP_MULTICAST_LOOP enabled or disabled as requested.
intf_name = find_ethernet_interface()
intf_index = socket.if_nametoindex(intf_name)
mcast_address = "ff02::abcd:99"
port = 30000
group = (mcast_address, port)
txsock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
txsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
txsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, intf_index)
if loopback_enabled:
txsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 1)
else:
txsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 0)
txsock.connect(group)
# Open a multicast receive socket and join the group
rxsock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
req = struct.pack("=16si", socket.inet_pton(socket.AF_INET6, mcast_address), intf_index)
if platform.system() == "Darwin":
rxsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, req)
else:
rxsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_ADD_MEMBERSHIP, req)
rxsock.bind(("::", port))
return (txsock, rxsock)
示例9: create_socket_ipv4_rx_ucast
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def create_socket_ipv4_rx_ucast(self):
if self._local_ipv4_address is None:
self.warning("Could not create IPv4 UDP socket: don't have a local address")
return None
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
except (IOError, OSError) as err:
self.warning("Could not create IPv4 UDP socket: %s", err)
return None
self.enable_addr_and_port_reuse(sock)
try:
sock.bind((self._local_ipv4_address, self._local_port))
except (IOError, OSError) as err:
self.warning("Could not bind IPv4 UDP socket to address %s port %d: %s",
self._local_ipv4_address, self._local_port, err)
return None
try:
sock.setblocking(0)
except (IOError, OSError) as err:
self.warning("Could set unicast receive IPv4 UDP to non-blocking mode: %s", err)
return None
return sock
示例10: create_socket_ipv6_tx_ucast
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def create_socket_ipv6_tx_ucast(self, remote_address, port):
try:
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
except IOError as err:
self.warning("Could not create IPv6 UDP socket: %s", err)
return None
self.enable_addr_and_port_reuse(sock)
try:
sock_addr = socket.getaddrinfo(remote_address, port, socket.AF_INET6,
socket.SOCK_DGRAM)[0][4]
sock.connect(sock_addr)
except IOError as err:
self.warning("Could not connect UDP socket to address %s port %d: %s",
remote_address, port, err)
return None
return sock
示例11: sendto
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def sendto(self, bytes, *args):
if self.type != socket.SOCK_DGRAM:
return _BaseSocket.sendto(self, bytes, *args)
if not self._proxyconn:
self.bind(("", 0))
address = args[-1]
flags = args[:-1]
header = BytesIO()
RSV = b"\x00\x00"
header.write(RSV)
STANDALONE = b"\x00"
header.write(STANDALONE)
self._write_SOCKS5_address(address, header)
sent = _BaseSocket.send(self, header.getvalue() + bytes, *flags)
return sent - header.tell()
示例12: recvfrom
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def recvfrom(self, bufsize, flags=0):
if self.type != socket.SOCK_DGRAM:
return _BaseSocket.recvfrom(self, bufsize, flags)
if not self._proxyconn:
self.bind(("", 0))
buf = BytesIO(_BaseSocket.recv(self, bufsize, flags))
buf.seek(+2, SEEK_CUR)
frag = buf.read(1)
if ord(frag):
raise NotImplementedError("Received UDP packet fragment")
fromhost, fromport = self._read_SOCKS5_address(buf)
peerhost, peerport = self.proxy_peername
filterhost = socket.inet_pton(self.family, peerhost).strip(b"\x00")
filterhost = filterhost and fromhost != peerhost
if filterhost or peerport not in (0, fromport):
raise socket.error(EAGAIN, "Packet filtered")
return (buf.read(), (fromhost, fromport))
示例13: worker
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def worker(self, thread_id):
print("About to fire the test case...")
if self._delay:
time.sleep(self._delay)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM if self._proto == "tcp" else socket.SOCK_DGRAM)
sock.settimeout(self._timeout)
sock.connect((self._host, self._port))
sock.send(self._data)
sock.recv(1024)
sock.close()
except Exception:
_l.error("Failed to feed network data to target %s:%d.", self._host, self._port, exc_info=True)
finally:
# Pop the thread object
self._threads.pop(thread_id, None)
示例14: send_packet
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def send_packet(secret):
try:
broadcast_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
broadcast_socket.bind(('', 0))
broadcast_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
except Exception as err:
print "[!] Error creating broadcast socket: %s" % err
sys.exit(ERROR)
data = "nukemyluks_" + secret
try:
broadcast_socket.sendto(data, ('<broadcast>', DEFAULT_PORT))
except Exception as err:
print "[!] Error sending packet: %s" % err
sys.exit(ERROR)
示例15: get_host_ip
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOCK_DGRAM [as 别名]
def get_host_ip(hostIP=None):
if hostIP is None or hostIP == 'auto':
hostIP = 'ip'
if hostIP == 'dns':
hostIP = socket.getfqdn()
elif hostIP == 'ip':
from socket import gaierror
try:
hostIP = socket.gethostbyname(socket.getfqdn())
except gaierror:
logger.warn('gethostbyname(socket.getfqdn()) failed... trying on hostname()')
hostIP = socket.gethostbyname(socket.gethostname())
if hostIP.startswith("127."):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# doesn't have to be reachable
s.connect(('10.255.255.255', 1))
hostIP = s.getsockname()[0]
return hostIP