http_post_file
demonstrates TCP/IP TXMODE via HTTP POST of an image¶
HTTP POST DEMO USING Python TCPIP TX MODE¶
This script demonstrates how TCPIP TX MODE can be used to send images to a server via HTTPS. The demonstration also shows how additional header information can be generated, in this case the current battery voltage. The demo requires a FAT32 formatted SD Card to be inserted in to the X-Link in order to hold the image.
The script will generate an HTTP header for a multipart POST in order to send the image file. The script will also generate some HTTP content that includes the name of the file.
Below is an example of the HTTP header and body generated by the script. In this example, the ‘image’ file is really a text file with only this data: 1234567890
POST /post HTTP/1.1 Host: httpbin.org Content-Type: multipart/form-data; boundary=Start-OttHydromet-Boundary-End Content-Length: 182 Battery-Voltage: 10.75
–Start-OttHydromet-Boundary-End Content-Disposition: form-data; name=”file”; filename=”test_image.jpg” Content-Type: image/jpeg
1234567890 –Start-OttHydromet-Boundary-End–
If you are using a version of LinkComm which will not let you set the TXMODE to Python TCPIP (but are running a version of firmware that supports it) you will need to click the “Test Script File…” button and the script will set the necessary fields. If you need to change the setup after that, be sure to click “Test Script File…” button again.
The demo will transmit any files in the /sd/tx1 folder. If you would like to create a fake image to send then run the script task create_image.
The public test server httpbin.org is used to perform the test transmission and verify the result. The last result can be displayed by running the script task display_results.
With the following TX1 setup, this script will transmit any images in the /sd/tx1 folder to httpbin.org test server every hour via secure HTTPS:
!TX1 Enable=On
!TX1 Radio Type=Cell
!TX1 Kind=Scheduled
!TX1 Label=HTTP POST
!TX1 Scheduled Interval=01:00:00
!TX1 Scheduled Time=00:00:00
!TX1 Data Source=File
!TX1 Max Files Per Tx=16
!TX1 File Expiry Period=10080 min
!TX1 Mode=Python TCPIP
!TX1 Secure=On
!TX1 Use Certificate=Off
!TX1 Mode Function=send_image_http
!TX1 Main Server=httpbin.org
!TX1 Backup Server=
!TX1 Server Port=443
!TX1 Server Username=
!TX1 Server Password=
!TX1 Server Path=post
You can attempt the transmission with HTTP rather than secure HTTPS by setting the following (not all modems support HTTPS):
!TX1 Secure=Off
!TX1 Server Port=80
-
http_post_file.
create_image
()¶ Create a fake image to send on the SD card. Set the simple variable in the function to True if you want a small 10 byte image, or to False if you want an image consisting of 16KB of random data.
-
http_post_file.
display_results
()¶ Display the last results received from the HTTP Server
-
http_post_file.
receive_data
(socket, amount=1024, iterations=120)¶ Receive data from a socket.
This function calls socket.recv(amount) up to ‘iterations’ times, concatenating and returning the result. It exits early if after receiving data on one iteration it doesn’t receive data on the next.
Parameters: - socket (socket.socket) – The socket object to receive data from.
- amount (int, optional) – The maximum amount of data to be received at once, defaults to 1024.
- iterations (int, optional) – The maximum number of iterations to attempt receiving data, defaults to 10.
Returns: The concatenated data received from the socket.
Return type:
-
http_post_file.
send_image_http
(socket, message, file_name)¶ Function to send an image file via HTTP POST to httpbin.org.
Parameters: - socket – Open socket for sending/receiving data.
- message – Unused (we expect a file transfer)
- file_name – Name of the file to be sent (‘image.jpg’ in this case).
Returns: 0 if HTTP POST was accepted and acknowledged, 1 if there was an issue.