Python Integration
This guide provides instructions for integrating the Fleetshare SDK into a Python application. This method leverages the asyncio
library to run the SDK concurrently with your main application logic.
Requirements
Before you begin, ensure you have the necessary libraries installed. You can install them using pip
:
asyncio
websockets
json
base64
time
random
string
Integration Steps
1. Import Required Libraries
Ensure you import the required Fleetshare SDK Python Library into your Project. The Fleetshare team will provide you with the necessary files.
2. Define Your Main Async Function
Define any asynchronous function where you will start the Fleetshare SDK and run your main program logic.
3. Start the Fleetshare SDK
Invoke the Fleetshare SDK within your asynchronous function. Use the provided API_KEY
and, if necessary, additional parameters like silent
and device_name
.
Parameters:
- API_KEY: Your API key from the Fleetshare SDK program.
- silent: (Optional) If set to
True
, the SDK runs silently without outputting logs. - device_name: (Optional) A unique name for the device running the SDK.
4. Implement Main Program Logic
Include your main program logic within the asynchronous function. The Fleetshare SDK will continue to share bandwidth in the background while your program runs.
Example Implementation
Here’s an example of integrating the Fleetshare SDK into a Python application:
async def main():
# Start Fleetshare SDK in the background
API_KEY = "YOUR_API_KEY"
sdk_task = asyncio.create_task(run_fleetshare_sdk_forever(API_KEY, silent=True, device_name="User123"))
# Your main program logic goes here
for x in range(10000):
await asyncio.sleep(1)
if __name__ == "__main__":
asyncio.run(main())
Best Practices
- Error Handling: Implement robust error handling within your asynchronous functions to manage potential issues.
- User Consent: Make sure to obtain user consent before initiating the bandwidth sharing process.
- Updating the SDK: Stay updated with new SDK versions and implement mechanisms to handle SDK updates gracefully, as older versions may become deprecated.
Ensure you follow these best practices to maintain a secure and user-friendly application.