Python ussl.wrap_socket() Examples
The following are 16
code examples of ussl.wrap_socket().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ussl
, or try the search function
.
Example #1
Source File: thingspeak.py From Micropython with MIT License | 9 votes |
def main(use_stream=True): while True: d.measure() data = b"api_key="+ API_KEY + "&field1=" + str(d.temperature()) + "&field2=" + str(d.humidity()) s = _socket.socket() ai = _socket.getaddrinfo(HOST, 443) addr = ai[0][-1] s.connect(addr) s = ssl.wrap_socket(s) s.write("POST /update HTTP/1.0\r\n") s.write("Host: " + HOST + "\r\n") s.write("Content-Length: " + str(len(data)) + "\r\n\r\n") s.write(data) print(s.read(128)) s.close() time.sleep(60)
Example #2
Source File: main.py From ai-smarthome with BSD 2-Clause "Simplified" License | 6 votes |
def http_get(url): print("Fetching:", url) proto, _, host, path = url.split('/', 3) if proto == "http:": port = 80 elif proto == "https:": port = 443 addr = socket.getaddrinfo(host, port)[0][-1] s = socket.socket() s.connect(addr) if proto == "https:": s = ussl.wrap_socket(s, server_hostname=host) s.write(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8')) while True: data = s.read(100) if data: print(str(data, 'utf8'), end='') else: break s.close() # Read config # should have { 'SSID': <YOUR_SSID>, 'password':<pass> }
Example #3
Source File: __init__.py From uPyCam with Apache License 2.0 | 5 votes |
def open_connection(host, port, ssl=False): if DEBUG and __debug__: log.debug("open_connection(%s, %s)", host, port) ai = _socket.getaddrinfo(host, port, 0, _socket.SOCK_STREAM) ai = ai[0] s = _socket.socket(ai[0], ai[1], ai[2]) s.setblocking(False) try: s.connect(ai[-1]) except OSError as e: if e.args[0] != uerrno.EINPROGRESS: raise if DEBUG and __debug__: log.debug("open_connection: After connect") yield IOWrite(s) # if __debug__: # assert s2.fileno() == s.fileno() if DEBUG and __debug__: log.debug("open_connection: After iowait: %s", s) if ssl: print("Warning: uasyncio SSL support is alpha") import ussl s.setblocking(True) s2 = ussl.wrap_socket(s) s.setblocking(False) return StreamReader(s, s2), StreamWriter(s2, {}) return StreamReader(s), StreamWriter(s, {})
Example #4
Source File: iothub.py From Micropython with MIT License | 5 votes |
def main(use_stream=True): # Ask for data d.measure() data = b"{'t': " + str(d.temperature()) + ", 'h': " + str(d.humidity()) + "}" s = socket.socket() ai = socket.getaddrinfo(HOST, 443) addr = ai[0][-1] s.connect(addr) # SSL wrap s = ussl.wrap_socket(s) # Send POST request to Azure IoT Hub s.write("POST /devices/" + DEVICE + "/messages/events?api-version=2016-02-03 HTTP/1.0\r\n") # HTTP Headers s.write("Host: " + HOST + "\r\n") s.write("Authorization: " + SAS + "\r\n") s.write("Content-Type: application/json\r\n") s.write("Connection: close\r\n") s.write("Content-Length: " + str(len(data)) + "\r\n\r\n") # Data s.write(data) # Print 128 bytes of response print(s.read(128)) s.close() # Run
Example #5
Source File: __init__.py From micropython-samples with MIT License | 5 votes |
def open_connection(host, port, ssl=False): if DEBUG and __debug__: log.debug("open_connection(%s, %s)", host, port) ai = _socket.getaddrinfo(host, port, 0, _socket.SOCK_STREAM) ai = ai[0] s = _socket.socket(ai[0], ai[1], ai[2]) s.setblocking(False) try: s.connect(ai[-1]) except OSError as e: if e.args[0] != uerrno.EINPROGRESS: raise if DEBUG and __debug__: log.debug("open_connection: After connect") yield IOWrite(s) # if __debug__: # assert s2.fileno() == s.fileno() if DEBUG and __debug__: log.debug("open_connection: After iowait: %s", s) if ssl: print("Warning: uasyncio SSL support is alpha") import ussl s.setblocking(True) s2 = ussl.wrap_socket(s) s.setblocking(False) return StreamReader(s, s2), StreamWriter(s2, {}) return StreamReader(s), StreamWriter(s, {})
Example #6
Source File: mqtt.py From upython-aq-monitor with MIT License | 5 votes |
def connect(self, clean_session=True): self.sock = socket.socket() self.sock.connect(self.addr) if self.ssl: import ussl self.sock = ussl.wrap_socket(self.sock, **self.ssl_params) msg = bytearray(b"\x10\0\0\x04MQTT\x04\x02\0\0") msg[1] = 10 + 2 + len(self.client_id) msg[9] = clean_session << 1 if self.user is not None: msg[1] += 2 + len(self.user) + 2 + len(self.pswd) msg[9] |= 0xC0 if self.keepalive: assert self.keepalive < 65536 msg[10] |= self.keepalive >> 8 msg[11] |= self.keepalive & 0x00FF if self.lw_topic: msg[1] += 2 + len(self.lw_topic) + 2 + len(self.lw_msg) msg[9] |= 0x4 | (self.lw_qos & 0x1) << 3 | (self.lw_qos & 0x2) << 3 msg[9] |= self.lw_retain << 5 self.sock.write(msg) #print(hex(len(msg)), hexlify(msg, ":")) self._send_str(self.client_id) if self.lw_topic: self._send_str(self.lw_topic) self._send_str(self.lw_msg) if self.user is not None: self._send_str(self.user) self._send_str(self.pswd) resp = self.sock.read(4) assert resp[0] == 0x20 and resp[1] == 0x02 if resp[3] != 0: raise MQTTException(resp[3]) return resp[2] & 1
Example #7
Source File: __init__.py From microdot with MIT License | 5 votes |
def open_connection(host, port, ssl=False): if DEBUG and __debug__: log.debug("open_connection(%s, %s)", host, port) ai = _socket.getaddrinfo(host, port, 0, _socket.SOCK_STREAM) ai = ai[0] s = _socket.socket(ai[0], ai[1], ai[2]) s.setblocking(False) try: s.connect(ai[-1]) except OSError as e: if e.args[0] != uerrno.EINPROGRESS: raise if DEBUG and __debug__: log.debug("open_connection: After connect") yield IOWrite(s) # if __debug__: # assert s2.fileno() == s.fileno() if DEBUG and __debug__: log.debug("open_connection: After iowait: %s", s) if ssl: print("Warning: uasyncio SSL support is alpha") import ussl s.setblocking(True) s2 = ussl.wrap_socket(s) s.setblocking(False) return StreamReader(s, s2), StreamWriter(s2, {}) return StreamReader(s), StreamWriter(s, {})
Example #8
Source File: umqtt.py From pyaiot with BSD 3-Clause "New" or "Revised" License | 5 votes |
def connect(self, clean_session=True): self.sock = socket.socket() self.sock.connect(self.addr) if self.ssl: import ussl self.sock = ussl.wrap_socket(self.sock, **self.ssl_params) msg = bytearray(b"\x10\0\0\x04MQTT\x04\x02\0\0") msg[1] = 10 + 2 + len(self.client_id) msg[9] = clean_session << 1 if self.user is not None: msg[1] += 2 + len(self.user) + 2 + len(self.pswd) msg[9] |= 0xC0 if self.keepalive: assert self.keepalive < 65536 msg[10] |= self.keepalive >> 8 msg[11] |= self.keepalive & 0x00FF if self.lw_topic: msg[1] += 2 + len(self.lw_topic) + 2 + len(self.lw_msg) msg[9] |= 0x4 | (self.lw_qos & 0x1) << 3 | (self.lw_qos & 0x2) << 3 msg[9] |= self.lw_retain << 5 self.sock.write(msg) #print(hex(len(msg)), hexlify(msg, ":")) self._send_str(self.client_id) if self.lw_topic: self._send_str(self.lw_topic) self._send_str(self.lw_msg) if self.user is not None: self._send_str(self.user) self._send_str(self.pswd) resp = self.sock.read(4) assert resp[0] == 0x20 and resp[1] == 0x02 if resp[3] != 0: raise MQTTException(resp[3]) return resp[2] & 1
Example #9
Source File: __init__.py From pysmartnode with MIT License | 5 votes |
def open_connection(host, port, ssl=False): if DEBUG and __debug__: log.debug("open_connection(%s, %s)", host, port) ai = _socket.getaddrinfo(host, port, 0, _socket.SOCK_STREAM) ai = ai[0] s = _socket.socket(ai[0], ai[1], ai[2]) s.setblocking(False) try: s.connect(ai[-1]) except OSError as e: if e.args[0] != uerrno.EINPROGRESS: raise if DEBUG and __debug__: log.debug("open_connection: After connect") yield IOWrite(s) # if __debug__: # assert s2.fileno() == s.fileno() if DEBUG and __debug__: log.debug("open_connection: After iowait: %s", s) if ssl: print("Warning: uasyncio SSL support is alpha") import ussl s.setblocking(True) s2 = ussl.wrap_socket(s) s.setblocking(False) return StreamReader(s, s2), StreamWriter(s2, {}) return StreamReader(s), StreamWriter(s, {})
Example #10
Source File: main.py From xbee-micropython with MIT License | 5 votes |
def https_test(hostname=AWS_ENDPOINT, sslp=SSL_PARAMS): """ Tests the HTTPS connectivity with AWS. Sends an HTTP request to obtain the shadow of a thing and prints it. :param hostname: AWS hostname to connect to. :param sslp: SSL certificate parameters. """ # Connect to AWS. s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM, usocket.IPPROTO_SEC) s.setblocking(False) w = ussl.wrap_socket(s, **sslp) print("- Connecting to AWS... ", end="") w.connect((hostname, 8443)) print("[OK]") # Send HTTP request. print("- Sending shadow request for thing '%s'... " % THING_NAME, end="") w.write(b'GET /things/%s/shadow HTTP/1.0\r\n' b'Host: %s\r\n' b'\r\n' % (THING_NAME, hostname)) print("[OK]") # Read answer. print("- Waiting for data... ", end="") while True: data = w.read(1024) if data: print("[OK]") print("- Received shadow for thing '%s':" % THING_NAME) print(64 * "-") print(str(data, 'utf-8')) print(64 * "-") break # Disconnect. w.close() print("- Done")
Example #11
Source File: __init__.py From micropython-async with MIT License | 5 votes |
def open_connection(host, port, ssl=False): if DEBUG and __debug__: log.debug("open_connection(%s, %s)", host, port) ai = _socket.getaddrinfo(host, port, 0, _socket.SOCK_STREAM) ai = ai[0] s = _socket.socket(ai[0], ai[1], ai[2]) s.setblocking(False) try: s.connect(ai[-1]) except OSError as e: if e.args[0] != uerrno.EINPROGRESS: raise if DEBUG and __debug__: log.debug("open_connection: After connect") yield IOWrite(s) # if __debug__: # assert s2.fileno() == s.fileno() if DEBUG and __debug__: log.debug("open_connection: After iowait: %s", s) if ssl: print("Warning: uasyncio SSL support is alpha") import ussl s.setblocking(True) s2 = ussl.wrap_socket(s) s.setblocking(False) return StreamReader(s, s2), StreamWriter(s2, {}) return StreamReader(s), StreamWriter(s, {})
Example #12
Source File: __init__.py From microhomie with MIT License | 5 votes |
def open_connection(host, port, ssl=False): if DEBUG and __debug__: log.debug("open_connection(%s, %s)", host, port) ai = _socket.getaddrinfo(host, port, 0, _socket.SOCK_STREAM) ai = ai[0] s = _socket.socket(ai[0], ai[1], ai[2]) s.setblocking(False) try: s.connect(ai[-1]) except OSError as e: if e.args[0] != uerrno.EINPROGRESS: raise if DEBUG and __debug__: log.debug("open_connection: After connect") yield IOWrite(s) # if __debug__: # assert s2.fileno() == s.fileno() if DEBUG and __debug__: log.debug("open_connection: After iowait: %s", s) if ssl: print("Warning: uasyncio SSL support is alpha") import ussl s.setblocking(True) s2 = ussl.wrap_socket(s) s.setblocking(False) return StreamReader(s, s2), StreamWriter(s2, {}) return StreamReader(s), StreamWriter(s, {})
Example #13
Source File: mqtt_as.py From micropython-mqtt with MIT License | 4 votes |
def _connect(self, clean): self._sock = socket.socket() self._sock.setblocking(False) try: self._sock.connect(self._addr) except OSError as e: if e.args[0] not in BUSY_ERRORS: raise await asyncio.sleep_ms(_DEFAULT_MS) self.dprint('Connecting to broker.') if self._ssl: import ussl self._sock = ussl.wrap_socket(self._sock, **self._ssl_params) premsg = bytearray(b"\x10\0\0\0\0\0") msg = bytearray(b"\x04MQTT\x04\0\0\0") # Protocol 3.1.1 sz = 10 + 2 + len(self._client_id) msg[6] = clean << 1 if self._user: sz += 2 + len(self._user) + 2 + len(self._pswd) msg[6] |= 0xC0 if self._keepalive: msg[7] |= self._keepalive >> 8 msg[8] |= self._keepalive & 0x00FF if self._lw_topic: sz += 2 + len(self._lw_topic) + 2 + len(self._lw_msg) msg[6] |= 0x4 | (self._lw_qos & 0x1) << 3 | (self._lw_qos & 0x2) << 3 msg[6] |= self._lw_retain << 5 i = 1 while sz > 0x7f: premsg[i] = (sz & 0x7f) | 0x80 sz >>= 7 i += 1 premsg[i] = sz await self._as_write(premsg, i + 2) await self._as_write(msg) await self._send_str(self._client_id) if self._lw_topic: await self._send_str(self._lw_topic) await self._send_str(self._lw_msg) if self._user: await self._send_str(self._user) await self._send_str(self._pswd) # Await CONNACK # read causes ECONNABORTED if broker is out; triggers a reconnect. resp = await self._as_read(4) self.dprint('Connected to broker.') # Got CONNACK if resp[3] != 0 or resp[0] != 0x20 or resp[1] != 0x02: raise OSError(-1) # Bad CONNACK e.g. authentication fail.
Example #14
Source File: simple.py From UIFlow-Code with GNU General Public License v3.0 | 4 votes |
def connect(self, clean_session=True): if self.socket_connect() == False: raise MQTTException('addr get error') self.sock.setblocking(True) if self.ssl: import ussl self.sock = ussl.wrap_socket(self.sock, **self.ssl_params) premsg = bytearray(b"\x10\0\0\0\0\0") msg = bytearray(b"\x04MQTT\x04\x02\0\0") sz = 10 + 2 + len(self.client_id) msg[6] = clean_session << 1 if self.user is not None: sz += 2 + len(self.user) + 2 + len(self.pswd) msg[6] |= 0xC0 if self.keepalive: assert self.keepalive < 65536 msg[7] |= self.keepalive >> 8 msg[8] |= self.keepalive & 0x00FF if self.lw_topic: sz += 2 + len(self.lw_topic) + 2 + len(self.lw_msg) msg[6] |= 0x4 | (self.lw_qos & 0x1) << 3 | (self.lw_qos & 0x2) << 3 msg[6] |= self.lw_retain << 5 i = 1 while sz > 0x7f: premsg[i] = (sz & 0x7f) | 0x80 sz >>= 7 i += 1 premsg[i] = sz self.sock.write(premsg, i + 2) self.sock.write(msg) #print(hex(len(msg)), hexlify(msg, ":")) self._send_str(self.client_id) if self.lw_topic: self._send_str(self.lw_topic) self._send_str(self.lw_msg) if self.user is not None: self._send_str(self.user) self._send_str(self.pswd) resp = self.sock.read(4) assert resp[0] == 0x20 and resp[1] == 0x02 if resp[3] != 0: raise MQTTException(resp[3]) return resp[2] & 1
Example #15
Source File: simple.py From xbee-micropython with MIT License | 4 votes |
def connect(self, clean_session=True): if self.ssl: proto = socket.IPPROTO_SEC else: proto = socket.IPPROTO_TCP self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, proto) if self.ssl: import ussl self.sock = ussl.wrap_socket(self.sock, **self.ssl_params) self.sock.connect((self.server, self.port)) premsg = bytearray(b"\x10\0\0\0\0\0") msg = bytearray(b"\x04MQTT\x04\x02\0\0") sz = 10 + 2 + len(self.client_id) msg[6] = clean_session << 1 if self.user is not None: sz += 2 + len(self.user) + 2 + len(self.pswd) msg[6] |= 0xC0 if self.keepalive: assert self.keepalive < 65536 msg[7] |= self.keepalive >> 8 msg[8] |= self.keepalive & 0x00FF if self.lw_topic: sz += 2 + len(self.lw_topic) + 2 + len(self.lw_msg) msg[6] |= 0x4 | (self.lw_qos & 0x1) << 3 | (self.lw_qos & 0x2) << 3 msg[6] |= self.lw_retain << 5 i = 1 while sz > 0x7f: premsg[i] = (sz & 0x7f) | 0x80 sz >>= 7 i += 1 premsg[i] = sz self.sock.write(premsg, i + 2) self.sock.write(msg) #print(hex(len(msg)), hexlify(msg, ":")) self._send_str(self.client_id) if self.lw_topic: self._send_str(self.lw_topic) self._send_str(self.lw_msg) if self.user is not None: self._send_str(self.user) self._send_str(self.pswd) resp = self.sock.read(4) assert resp[0] == 0x20 and resp[1] == 0x02 if resp[3] != 0: raise MQTTException(resp[3]) return resp[2] & 1
Example #16
Source File: mqtt_as.py From microhomie with MIT License | 4 votes |
def _connect(self, clean): self._sock = socket.socket() self._sock.setblocking(False) try: self._sock.connect(self._addr) except OSError as e: if e.args[0] not in BUSY_ERRORS: raise await asyncio.sleep_ms(_DEFAULT_MS) self.dprint('Connecting to broker.') if self._ssl: import ussl self._sock = ussl.wrap_socket(self._sock, **self._ssl_params) premsg = bytearray(b"\x10\0\0\0\0\0") msg = bytearray(b"\x04MQTT\x04\0\0\0") # Protocol 3.1.1 sz = 10 + 2 + len(self._client_id) msg[6] = clean << 1 if self._user: sz += 2 + len(self._user) + 2 + len(self._pswd) msg[6] |= 0xC0 if self._keepalive: msg[7] |= self._keepalive >> 8 msg[8] |= self._keepalive & 0x00FF if self._lw_topic: sz += 2 + len(self._lw_topic) + 2 + len(self._lw_msg) msg[6] |= 0x4 | (self._lw_qos & 0x1) << 3 | (self._lw_qos & 0x2) << 3 msg[6] |= self._lw_retain << 5 i = 1 while sz > 0x7f: premsg[i] = (sz & 0x7f) | 0x80 sz >>= 7 i += 1 premsg[i] = sz await self._as_write(premsg, i + 2) await self._as_write(msg) await self._send_str(self._client_id) if self._lw_topic: await self._send_str(self._lw_topic) await self._send_str(self._lw_msg) if self._user: await self._send_str(self._user) await self._send_str(self._pswd) # Await CONNACK # read causes ECONNABORTED if broker is out; triggers a reconnect. resp = await self._as_read(4) self.dprint('Connected to broker.') # Got CONNACK if resp[3] != 0 or resp[0] != 0x20 or resp[1] != 0x02: raise OSError(-1) # Bad CONNACK e.g. authentication fail.