Jump to content

Download file generated from a CGI script


Recommended Posts

I have found many examples to download files from the internet and IP cameras, however I cannot seem to get any of these to work.

We have this IP camera on our production floor that scans barcodes and takes a snapshot of each one and any errors that occur, such as a missing barcode. What I'm trying to do is have an autoit script download the images (save to a file) and display them on the screen. However, I cannot seem to figure out how to download the file. The camera saves pictures in RAW image format and I can access the picture using a URL like this: http://192.168.2.2/image.cgi?img=curr.raw&type=1&scale=1&bits=8 OR http://192.168.2.2/image.cgi?img=err01.raw&type=1&scale=1&bits=8 the camera then converts the image from RAW to .gif and it downloads (in the browser of course)

 

Any Ideas?

 

Func downloadfile()
   TCPStartup ()
   $Host = TCPNameToIP ( "http://192.168.2.2" )
   $downloadSocket = TCPConnect ( $Host, "80" )

   TCPSend($downloadSocket, _
        "GET /image.cgi?img=curr.raw&type=1&scale=1&bits=8 HTTP/1.1" & @CRLF & _
        "Host: " & $Host & ":" & "80" & @CRLF & _
        "Connection: keep-alive" & @CRLF & @CRLF)


        ; Assign a Local variable the handle of the file opened in binary overwrite mode.
    Local $hFile = FileOpen(@ScriptDir & "curr.gif", BitOR($FO_BINARY, $FO_OVERWRITE))

    ; Assign Locales Constant variables the number representing 4 KiB; the binary code for the end of the file and the length of the binary code.
    Local Const $i4KiB = 4096, $bEOF = Binary(@CRLF & "{EOF}"), $iEOFLen = BinaryLen($bEOF)

    ; Assign a Local variable the empty binary data which will contain the binary data of the file.
    Local $bData = Binary("")

    ; Assign a Local variable to store the length of the data received.
    Local $iDataLen = 0

    ; Assign a Local variable a boolean.
    Local $bEOFReached = False

    Do
        $bData = TCPRecv($downloadSocket, $i4KiB, 1)

        ; If an error occurred display the error code and return False.
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Connection lost, Error code: " & $iError)
            Return False
        EndIf

        $iDataLen = BinaryLen($bData)

        ; If nothing is received, retry for the incoming data.
        If $iDataLen = 0 Then ContinueLoop

        ; If the end of the file is reached.
        If BinaryMid($bData, 1 + $iDataLen - $iEOFLen, $iEOFLen) = $bEOF Then
            ; Strip the EOF code from the file data.
            $bData = BinaryMid($bData, 1, $iDataLen - $iEOFLen)

            ; Set the EOFReached to True.
            $bEOFReached = True
        EndIf

        FileWrite($hFile, $bData)
     Until $bEOFReached
     ; Close the file handle.
    FileClose($hFile)

    ; Display the successful message.
    MsgBox($MB_SYSTEMMODAL, "", "Client:" & @CRLF & "File received.")

    ; Close the socket.
    TCPCloseSocket($downloadSocket)

EndFunc

 

Edited by LogicDJTM
Link to comment
Share on other sites

You use functionTCPNameToIP (Converts an Internet name to IP address) to get the IP why? This function returns with @error, just test this script:

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Start the TCP service.
    TCPStartup()

    ; Register OnAutoItExit to be called when the script is closed.
    OnAutoItExitRegister("OnAutoItExit")

    ; Assign a Local variable the IP address of www.autoitscript.com.
    Local $sIPAddress = TCPNameToIP("http://192.168.2.2")

    ; If an error occurred display the error code and return False.
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "Error code: " & @error)
        Return False
    Else
        ; Display the IP address.
        MsgBox($MB_SYSTEMMODAL, "", "Domain name to IP :" & $sIPAddress)
    EndIf
EndFunc   ;==>Example

Func OnAutoItExit()
    TCPShutdown() ; Close the TCP service.
EndFunc   ;==>OnAutoItExit

 

Link to comment
Share on other sites

Why? well I don't know. I have been changing things to try and get it to work. It doesn't error if you remove the http:// and I have also tried assigning the IP directly, and it still fails. Just not sure where.

$downloadSocket = TCPConnect ( "192.168.2.2", "80" )

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...