def resolve(domain, is_ipv6): buf = uio.BytesIO(48) udnspkt.make_req(buf, "google.com", is_ipv6) v = buf.getvalue() print("query: ", v) s.sendto(v, dns_addr) resp = s.recv(1024) print("resp:", resp) buf = uio.BytesIO(resp) addr = udnspkt.parse_resp(buf, is_ipv6) print("bin addr:", addr) print("addr:", usocket.inet_ntop(usocket.AF_INET6 if is_ipv6 else usocket.AF_INET, addr))
def get_request(self): """Get the request and client address from the socket. May be overridden. """ try: micropython_socket except NameError: return self.socket.accept() else: s, addr = self.socket.accept() addr = socket.sockaddr(addr) return (s, (socket.inet_ntop(addr[0], addr[1]), addr[2]))
def makeport(self): err = None sock = None if self.source_address and self.source_address[0]: host = self.source_address[0] else: host = "127.0.0.1" if self.af == _socket.AF_INET else "::1" for port in range(MIN_PORT, MAX_PORT): addrinfo = _socket.getaddrinfo(host, port, self.af) for af, socktype, proto, _, addr in addrinfo: if af == self.af and socktype == _socket.SOCK_STREAM: try: sock = socket(af, socktype, proto) sock.bind(addr) except OSError as _: err = _ if sock: sock.close() sock = None continue else: try: sock.family = af except: pass if isinstance(addr, tuple): host = addr[0] else: try: host = _socket.inet_ntop(af, addr[4:8]) except: pass break if sock: break if sock is None: if err is not None: raise err else: raise OSError("getaddrinfo returns an empty list") sock.listen(1) if self.af == _socket.AF_INET: self.sendport(host, port) else: self.sendeprt(host, port) if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT: sock.settimeout(self.timeout) return sock
def resolve(domain, is_ipv6): buf = uio.BytesIO(48) udnspkt.make_req(buf, "google.com", is_ipv6) v = buf.getvalue() print("query: ", v) s.sendto(v, dns_addr) resp = s.recv(1024) print("resp:", resp) buf = uio.BytesIO(resp) addr = udnspkt.parse_resp(buf, is_ipv6) print("bin addr:", addr) print( "addr:", usocket.inet_ntop(usocket.AF_INET6 if is_ipv6 else usocket.AF_INET, addr))
def makeport(self): """Create a new socket and send a PORT command for it.""" err = None sock = None if self.source_address and self.source_address[0]: host = self.source_address[0] else: # XXX: this will only work for connections to a server on the same # host! socket.getsocketname() would be needed find out the # correct socket address to report to the server host = "127.0.0.1" if self.af == _socket.AF_INET else "::1" for port in range(MIN_PORT, MAX_PORT): addrinfo = _socket.getaddrinfo(host, port) for af, socktype, proto, _, addr in addrinfo: if af == self.af and socktype == _socket.SOCK_STREAM: try: sock = socket(af, socktype, proto) sock.bind(addr) except OSError as _: err = _ if sock: sock.close() sock = None continue else: try: sock.family = af except: pass if isinstance(addr, tuple): host = addr[0] else: try: # XXX: socket.inet_ntop() is not supported on # all MicroPython ports! host = _socket.inet_ntop(af, addr[4:8]) except: pass break if sock: break if sock is None: if err is not None: raise err else: raise OSError("getaddrinfo returns an empty list") sock.listen(1) if self.af == _socket.AF_INET: self.sendport(host, port) else: self.sendeprt(host, port) if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT: sock.settimeout(self.timeout) return sock
def accept(self): s, addr = super().accept() addr = _socket.sockaddr(addr) return (s, (_socket.inet_ntop(addr[0], addr[1]), addr[2]))
def recvfrom(self, *a, **b): s, addr = super().recvfrom(*a, **b) addr = _socket.sockaddr(addr) return (s, (_socket.inet_ntop(addr[0], addr[1]), addr[2]))