def _connect(host, port, scheme, ctx): try: #print("_connect",host,port,scheme) ip = __default_net["sock"][0].gethostbyname(host) #print(ip) if port: # port port = int(port) elif scheme == "http": port = 80 else: port = 443 ip = (ip, port) if scheme == "http": sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: if ctx is None: ctx = () sock = ssl.sslsocket(ctx=ctx) # print(ip) sock.connect(ip) return sock except ConnectionError as e: sock.close() raise e except IOError: #print("IOError") raise HTTPConnectionError except Exception as e: raise e
def __init__(self, ipv6=False): self._sockname = None self.sock = socket( AF_INET6 if ipv6 else AF_INET, SOCK_STREAM ) self.ssock = sslsocket(self.sock)
def _ll_connect(self): ip = __default_net["sock"][0].gethostbyname(self._host) if self._ssl_ctx is None: self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: self._sock = ssl.sslsocket(ctx=self._ssl_ctx) if self._sock_keepalive and len(self._sock_keepalive) == 3: try: self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) except Exception as e: print("Ignoring socket options", e) # no keepalive pass try: self._sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, self._sock_keepalive[0]) except Exception as e: print("Ignoring socket options", e) # no keepalive pass try: self._sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, self._sock_keepalive[1]) except Exception as e: print("Ignoring socket options", e) # no keepalive pass try: self._sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, self._sock_keepalive[2]) except Exception as e: print("Ignoring socket options", e) # no keepalive pass self._sock.connect((ip, self._port)) exc = None try: self._return_code = _mqtt_connect(self._sock.channel, self._keepalive) if self._return_code == RC_ACCEPTED: self._disconnected = False except Exception as e: #close the socket, ignoring exc self._disconnected = True exc = e if not _mqtt_connected(): self._close() if exc is not None: raise exc if _mqtt_connected() and self._after_connect is not None: self._after_connect(self) return self._return_code
def _connect(self, host, keepalive, port=PORT, ssl_ctx=None, breconnect_cb=None, aconnect_cb=None, sock_keepalive=None): self._before_reconnect = breconnect_cb self._after_connect = aconnect_cb self._host = host self._port = port ip = __default_net["sock"][0].gethostbyname(host) self._ssl_ctx = ssl_ctx if ssl_ctx is None: self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: self._sock = ssl.sslsocket(ctx=ssl_ctx) try: self._sock.settimeout(keepalive * 1000) except Exception as e: print(e) # no timeout pass if sock_keepalive and len(sock_keepalive) == 3: try: self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) except Exception as e: print("Ignoring socket options", e) # no keepalive pass try: self._sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, sock_keepalive[0]) except Exception as e: print("Ignoring socket options", e) # no keepalive pass try: self._sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, sock_keepalive[1]) except Exception as e: print("Ignoring socket options", e) # no keepalive pass try: self._sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, sock_keepalive[2]) except Exception as e: print("Ignoring socket options", e) # no keepalive pass try: self._sock.connect((ip, port)) except Exception as e: self._sock.close() raise e # protocol_name(str) + protocol_level + flags + keepalive + client_id(str) remaining_length = 2 + 4 + 1 + 1 + 2 + 2 + len(self._client_id) connect_flags = 0 if self.clean_session: connect_flags = connect_flags | 0x02 if self._will_topic: if self._will_payload is not None: remaining_length = remaining_length + 2 + len( self._will_topic) + 2 + len(self._will_payload) else: remaining_length = remaining_length + 2 + len( self._will_topic) + 2 connect_flags = connect_flags | 0x04 | ( (self._will_qos & 0x03) << 3) | ( (self._will_retain & 0x01) << 5) if self._username: remaining_length = remaining_length + 2 + len(self._username) connect_flags = connect_flags | 0x80 if self._password: connect_flags = connect_flags | 0x40 remaining_length = remaining_length + 2 + len(self._password) packet = bytearray() #fixed header packet.append(CONNECT) self._pack_remaining_length(packet, remaining_length) # variable header self._pack_str16(packet, PROTOCOL_NAMEv311) # protocol name packet.append(MQTTv311) # protocol level packet.append(connect_flags) _append16(packet, keepalive) self._pack_str16(packet, self._client_id) if self._will_topic: self._pack_str16(packet, self._will_topic) # CHECK!! if self._will_payload: self._pack_str16(packet, self._will_payload) if self._username: self._pack_str16(packet, self._username) if self._password: self._pack_str16(packet, self._password) self.keepalive = keepalive self._sock.sendall(packet) self._handle_connack() self._update_last_activity(LA_BOTH) if self._after_connect is not None: self._after_connect(self) rc = 0 # code below for reconnection # at the moment rc is always set to None by self._send_publish: useless self._out_message_lock.acquire() for m in self._out_messages: m.timestamp = timers.now() if m.qos == 0: rc = self._send_publish(m.mid, m.topic, m.payload, m.qos, m.retain, m.dup) if rc != 0: self._out_message_lock.release() return rc elif m.qos == 1: if m.state == mqtt_ms_publish: m.state = mqtt_ms_wait_for_puback rc = self._send_publish(m.mid, m.topic, m.payload, m.qos, m.retain, m.dup) if rc != 0: self._out_message_lock.release() return rc elif m.qos == 2: if m.state == mqtt_ms_publish: m.state = mqtt_ms_wait_for_pubrec rc = self._send_publish(m.mid, m.topic, m.payload, m.qos, m.retain, m.dup) if rc != 0: self._out_message_lock.release() return rc elif m.state == mqtt_ms_resend_pubrel: m.state = mqtt_ms_wait_for_pubcomp rc = self._send_pubrel(m.mid, m.dup) if rc != 0: self._out_message_lock.release() return rc self._out_message_lock.release() return MQTT_ERR_SUCCESS
def _connect(self, host, keepalive, port=PORT, ssl_ctx=None, breconnect_cb=None, aconnect_cb=None, sock_keepalive=None): self._before_reconnect = breconnect_cb self._after_connect = aconnect_cb self._host = host self._port = port ip = __default_net["sock"][0].gethostbyname(host) self._ssl_ctx = ssl_ctx if ssl_ctx is None: self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: self._sock = ssl.sslsocket(ctx=ssl_ctx) try: self._sock.settimeout(keepalive*1000) except Exception as e: print(e) # no timeout pass if sock_keepalive and len(sock_keepalive) == 3: try: self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) except Exception as e: print(e) # no keepalive pass try: self._sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, sock_keepalive[0]) except Exception as e: print(e) # no keepalive pass try: self._sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, sock_keepalive[1]) except Exception as e: print(e) # no keepalive pass try: self._sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, sock_keepalive[2]) except Exception as e: print(e) # no keepalive pass try: self._sock.connect((ip,port)) except Exception as e: self._sock.close() raise e # protocol_name(str) + protocol_level + flags + keepalive + client_id(str) remaining_length = 2+4 + 1+1+2 + 2+len(self._client_id) connect_flags = 0 if self.clean_session: connect_flags = connect_flags | 0x02 if self._will_topic: if self._will_payload is not None: remaining_length = remaining_length + 2+len(self._will_topic) + 2+len(self._will_payload) else: remaining_length = remaining_length + 2+len(self._will_topic) + 2 connect_flags = connect_flags | 0x04 | ((self._will_qos & 0x03) << 3) | ((self._will_retain & 0x01) << 5) if self._username: remaining_length = remaining_length + 2+len(self._username) connect_flags = connect_flags | 0x80 if self._password: connect_flags = connect_flags | 0x40 remaining_length = remaining_length + 2+len(self._password) packet = bytearray() #fixed header packet.append(CONNECT) self._pack_remaining_length(packet, remaining_length) # variable header self._pack_str16(packet,PROTOCOL_NAMEv311) # protocol name packet.append(MQTTv311) # protocol level packet.append(connect_flags) _append16(packet, keepalive) self._pack_str16(packet, self._client_id) if self._will_topic: self._pack_str16(packet, self._will_topic) # CHECK!! if self._will_payload: self._pack_str16(packet, self._will_payload) if self._username: self._pack_str16(packet, self._username) if self._password: self._pack_str16(packet, self._password) self.keepalive = keepalive self._sock.sendall(packet) self._handle_connack() self._update_last_activity(LA_BOTH) if self._after_connect is not None: self._after_connect(self) rc = 0 # code below for reconnection # at the moment rc is always set to None by self._send_publish: useless self._out_message_lock.acquire() for m in self._out_messages: m.timestamp = timers.now() if m.qos == 0: rc = self._send_publish(m.mid, m.topic, m.payload, m.qos, m.retain, m.dup) if rc != 0: self._out_message_lock.release() return rc elif m.qos == 1: if m.state == mqtt_ms_publish: m.state = mqtt_ms_wait_for_puback rc = self._send_publish(m.mid, m.topic, m.payload, m.qos, m.retain, m.dup) if rc != 0: self._out_message_lock.release() return rc elif m.qos == 2: if m.state == mqtt_ms_publish: m.state = mqtt_ms_wait_for_pubrec rc = self._send_publish(m.mid, m.topic, m.payload, m.qos, m.retain, m.dup) if rc != 0: self._out_message_lock.release() return rc elif m.state == mqtt_ms_resend_pubrel: m.state = mqtt_ms_wait_for_pubcomp rc = self._send_pubrel(m.mid, m.dup) if rc != 0: self._out_message_lock.release() return rc self._out_message_lock.release() return MQTT_ERR_SUCCESS