Difference between revisions of "WGET CGI Post"

From Noah.org
Jump to navigationJump to search
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:Engineering]]
 +
[[Category:Python]]
 +
[[Category:PHP]]
 
== Post upload using wget ==
 
== Post upload using wget ==
  
I like to use wget in shell scripts to upload files over http
+
I like to use wget in shell scripts to upload files to remote servers over http.  
to remote servers. I use this when it isn't convenient to use
+
I use this when it isn't convenient to use scp.
scp or ftp.  
 
  
This requires a little python script on the client side.
+
This is not secure if you don't pay attention.
The script is to urlencode the file to be uploaded.
+
 
It's a very simple script, so you can easily replace it with the  
+
This requires a tiny Python script on the client side and a little PHP on the server side.
language of your choice.  
+
The Python script is used to encode the file to be uploaded so that wget can send it as a POST request.
 +
It's a very simple script, so you can easily replace it with the language of your choice (Perl or maybe even sh).  
  
 
Step 1. On the client side create a Python script called "postencode.py":
 
Step 1. On the client side create a Python script called "postencode.py":
 
<pre>
 
<pre>
    #!/bin/env python
+
#!/usr/bin/env python
    import sys, urllib, base64
+
import sys, urllib, base64
    input_filename = sys.argv[1]
+
input_filename = sys.argv[1]
    postwad_filename = input_filename + ".post"
+
postwad_filename = input_filename + ".post"
    datawad = base64.encodestring(file(input_filename, "rb").read())
+
datawad = base64.encodestring(file(input_filename, "rb").read())
    postwad = urllib.urlencode({"filedata":datawad, "filename":input_filename})
+
postwad = urllib.urlencode({"filedata":datawad, "filename":input_filename})
    file(postwad_filename, "wb").write(postwad)
+
file(postwad_filename, "wb").write(postwad)
    print postwad_filename
+
print postwad_filename
 
</pre>
 
</pre>
  
 
Step 2. On the server side create a PHP create a script called "upload.php":
 
Step 2. On the server side create a PHP create a script called "upload.php":
<pre>
 
    <html><head><title>Post Upload Tool</title><body>
 
    <?php
 
        $filename = $_POST['filename'];
 
        $filedata = base64_decode($_POST['filedata']);
 
        echo "<h1>$filename</h1>";
 
        $fout = fopen($filename,"wb");
 
        fwrite($fout, $filedata);
 
        fclose($fout);
 
    ?>
 
    </body></html>
 
</pre>
 
 
Step 3. Use wget to post a file to the server (note the backticks):
 
<pre>
 
    wget --post-file=`./postencode.py FILENAME` http://www.example.com/upload.php
 
</pre>
 
  
If you choose not to use Python for the postencode script then here are
+
<pre><nowiki>
references for other languages.
+
<html><head><title>Post Upload Tool</title><body>
 
+
<?php
In Perl use:
+
     $filename = $_POST['filename'];
     use URI::Escape;
+
     $filedata = base64_decode($_POST['filedata']);
     uri_escape(...);
+
     echo "<h1>$filename</h1>";
In PHP use:
+
    $fout = fopen($filename,"wb");
     urlencode(...);
+
    fwrite($fout, $filedata);
 
+
    fclose($fout);
<h1>$filename</h1>";
+
?>
        $fout = fopen($filename,"wb");
+
</body></html>
        fwrite($fout, $filedata);
+
</nowiki></pre>
        fclose($fout);
 
    ?>
 
    </body></html>
 
</pre>
 
  
 
Step 3. Use wget to post a file to the server (note the backticks):
 
Step 3. Use wget to post a file to the server (note the backticks):
 
<pre>
 
<pre>
    wget --post-file=`./postencode.py FILENAME` http://www.example.com/upload.php
+
wget --post-file=`./postencode.py FILENAME` http://www.example.com/upload.php
 
</pre>
 
</pre>
  
 +
== Other languages for the client side ==
 
If you choose not to use Python for the postencode script then here are
 
If you choose not to use Python for the postencode script then here are
 
references for other languages.
 
references for other languages.

Latest revision as of 17:36, 6 June 2007

Post upload using wget

I like to use wget in shell scripts to upload files to remote servers over http. I use this when it isn't convenient to use scp.

This is not secure if you don't pay attention.

This requires a tiny Python script on the client side and a little PHP on the server side. The Python script is used to encode the file to be uploaded so that wget can send it as a POST request. It's a very simple script, so you can easily replace it with the language of your choice (Perl or maybe even sh).

Step 1. On the client side create a Python script called "postencode.py":

#!/usr/bin/env python
import sys, urllib, base64
input_filename = sys.argv[1]
postwad_filename = input_filename + ".post"
datawad = base64.encodestring(file(input_filename, "rb").read())
postwad = urllib.urlencode({"filedata":datawad, "filename":input_filename})
file(postwad_filename, "wb").write(postwad)
print postwad_filename

Step 2. On the server side create a PHP create a script called "upload.php":

<html><head><title>Post Upload Tool</title><body>
<?php
    $filename = $_POST['filename'];
    $filedata = base64_decode($_POST['filedata']);
    echo "<h1>$filename</h1>";
    $fout = fopen($filename,"wb");
    fwrite($fout, $filedata);
    fclose($fout);
?>
</body></html>

Step 3. Use wget to post a file to the server (note the backticks):

wget --post-file=`./postencode.py FILENAME` http://www.example.com/upload.php

Other languages for the client side

If you choose not to use Python for the postencode script then here are references for other languages.

In Perl use:

   use URI::Escape;
   uri_escape(...);

In PHP use:

   urlencode(...);