async def main(): # The connection string for a device should never be stored in code. For the sake of simplicity we're using an environment variable here. conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING") proxy_opts = ProxyOptions( proxy_type=socks.HTTP, proxy_addr="127.0.0.1", proxy_port=8888 # localhost ) # The client object is used to interact with your Azure IoT hub. device_client = IoTHubDeviceClient.create_from_connection_string( conn_str, websockets=True, proxy_options=proxy_opts ) # Connect the client. await device_client.connect() async def send_test_message(i): print("sending message #" + str(i)) msg = Message("test wind speed " + str(i)) msg.message_id = uuid.uuid4() msg.correlation_id = "correlation-1234" msg.custom_properties["tornado-warning"] = "yes" msg.content_encoding = "utf-8" msg.content_type = "application/json" await device_client.send_message(msg) print("done sending message #" + str(i)) # send `messages_to_send` messages in parallel await asyncio.gather(*[send_test_message(i) for i in range(1, messages_to_send + 1)]) # Finally, shut down the client await device_client.shutdown()
def test_proxy_options(self, mocker, required_kwargs, config_cls, sastoken): proxy_options = ProxyOptions(proxy_type=mocker.MagicMock(), proxy_addr="127.0.0.1", proxy_port=8888) config = config_cls(sastoken=sastoken, proxy_options=proxy_options, **required_kwargs) assert config.proxy_options is proxy_options
def test_proxy_options(self, client_create_method, create_method_args, mock_pipeline_init): proxy_options = ProxyOptions(proxy_type=socks.HTTP, proxy_addr="127.0.0.1", proxy_port=8888) client_create_method(*create_method_args, proxy_options=proxy_options) # Get configuration object assert mock_pipeline_init.call_count == 1 config = mock_pipeline_init.call_args[0][0] assert isinstance(config, ProvisioningPipelineConfig) assert config.proxy_options is proxy_options
def test_proxy_options( self, option_test_required_patching, client_create_method, create_method_args, mock_mqtt_pipeline_init, mock_http_pipeline_init, ): proxy_options = ProxyOptions(proxy_type=socks.HTTP, proxy_addr="127.0.0.1", proxy_port=8888) client_create_method(*create_method_args, proxy_options=proxy_options) # Get configuration object, and ensure it was used for both protocol pipelines assert mock_mqtt_pipeline_init.call_count == 1 config = mock_mqtt_pipeline_init.call_args[0][0] assert isinstance(config, IoTHubPipelineConfig) assert config == mock_http_pipeline_init.call_args[0][0] assert config.proxy_options is proxy_options
import os import time import uuid from azure.iot.device import IoTHubDeviceClient, Message, ProxyOptions import socks import logging logging.basicConfig(level=logging.DEBUG) # The connection string for a device should never be stored in code. For the sake of simplicity we're using an environment variable here. conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING") # Create proxy options when trying to send via proxy proxy_opts = ProxyOptions( proxy_type=socks.HTTP, proxy_addr="127.0.0.1", proxy_port=8888 # localhost ) # The client object is used to interact with your Azure IoT hub. device_client = IoTHubDeviceClient.create_from_connection_string( conn_str, websockets=True, proxy_options=proxy_opts) # Connect the client. device_client.connect() # send 2 messages with 2 system properties & 1 custom property with a 1 second pause between each message for i in range(1, 3): print("sending message #" + str(i)) msg = Message("test wind speed " + str(i)) msg.message_id = uuid.uuid4() msg.correlation_id = "correlation-1234"
def test_proxy_options(self, mocker, config_cls): proxy_options = ProxyOptions( proxy_type=mocker.MagicMock(), proxy_addr="127.0.0.1", proxy_port=8888 ) config = config_cls(proxy_options=proxy_options) assert config.proxy_options is proxy_options
# -------------------------------------------------------------------------- import os import time import uuid from azure.iot.device import IoTHubDeviceClient, Message, ProxyOptions import logging logging.basicConfig(level=logging.DEBUG) # The connection string for a device should never be stored in code. For the sake of simplicity we're using an environment variable here. conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING") # Create proxy options when trying to send via proxy proxy_opts = ProxyOptions(proxy_type="HTTP", proxy_addr="127.0.0.1", proxy_port=8888) # localhost # The client object is used to interact with your Azure IoT hub. device_client = IoTHubDeviceClient.create_from_connection_string( conn_str, websockets=True, proxy_options=proxy_opts) # Connect the client. device_client.connect() # send 2 messages with 2 system properties & 1 custom property with a 1 second pause between each message for i in range(1, 3): print("sending message #" + str(i)) msg = Message("test wind speed " + str(i)) msg.message_id = uuid.uuid4() msg.correlation_id = "correlation-1234" msg.custom_properties["tornado-warning"] = "yes"