当前位置: 首页>>代码示例>>Python>>正文


Python ussl.wrap_socket方法代码示例

本文整理汇总了Python中ussl.wrap_socket方法的典型用法代码示例。如果您正苦于以下问题:Python ussl.wrap_socket方法的具体用法?Python ussl.wrap_socket怎么用?Python ussl.wrap_socket使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ussl的用法示例。


在下文中一共展示了ussl.wrap_socket方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: import ussl [as 别名]
# 或者: from ussl import wrap_socket [as 别名]
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) 
开发者ID:bechynsky,项目名称:Micropython,代码行数:23,代码来源:thingspeak.py

示例2: http_get

# 需要导入模块: import ussl [as 别名]
# 或者: from ussl import wrap_socket [as 别名]
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> } 
开发者ID:joakimeriksson,项目名称:ai-smarthome,代码行数:27,代码来源:main.py

示例3: open_connection

# 需要导入模块: import ussl [as 别名]
# 或者: from ussl import wrap_socket [as 别名]
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, {}) 
开发者ID:lemariva,项目名称:uPyCam,代码行数:29,代码来源:__init__.py

示例4: main

# 需要导入模块: import ussl [as 别名]
# 或者: from ussl import wrap_socket [as 别名]
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 
开发者ID:bechynsky,项目名称:Micropython,代码行数:33,代码来源:iothub.py

示例5: connect

# 需要导入模块: import ussl [as 别名]
# 或者: from ussl import wrap_socket [as 别名]
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 
开发者ID:ayoy,项目名称:upython-aq-monitor,代码行数:36,代码来源:mqtt.py

示例6: open_connection

# 需要导入模块: import ussl [as 别名]
# 或者: from ussl import wrap_socket [as 别名]
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, {}) 
开发者ID:kevinkk525,项目名称:pysmartnode,代码行数:29,代码来源:__init__.py

示例7: https_test

# 需要导入模块: import ussl [as 别名]
# 或者: from ussl import wrap_socket [as 别名]
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") 
开发者ID:digidotcom,项目名称:xbee-micropython,代码行数:39,代码来源:main.py

示例8: _connect

# 需要导入模块: import ussl [as 别名]
# 或者: from ussl import wrap_socket [as 别名]
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. 
开发者ID:peterhinch,项目名称:micropython-mqtt,代码行数:52,代码来源:mqtt_as.py

示例9: connect

# 需要导入模块: import ussl [as 别名]
# 或者: from ussl import wrap_socket [as 别名]
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 
开发者ID:m5stack,项目名称:UIFlow-Code,代码行数:48,代码来源:simple.py

示例10: connect

# 需要导入模块: import ussl [as 别名]
# 或者: from ussl import wrap_socket [as 别名]
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 
开发者ID:digidotcom,项目名称:xbee-micropython,代码行数:51,代码来源:simple.py


注:本文中的ussl.wrap_socket方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。