title: Send file using POST from a Python script to a PHP Script
body: <pre><code>import urllib,MultipartPostHandler,urllib2,cookielib
  cookies = cookielib.CookieJar()
  opener =     urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),MultipartPostHandler.MultipartPostHandler)
  urllib2.install_opener(opener)
  login = urllib.urlencode(dict(admin_user='admin',admin_pass='****'))
  o=opener.open('http://some_domain_name/admin/index.php',login)
  print o.read()
  ### successfully logged-into the system ###################
  </code></pre>
  
  <p>With the help of above code I am able to log into admin panel. Here I am supposed to post some ads (Here I am automating the tasks and the fields covering ad-title, ad-desc, and mainly importantly series of images (list of images would be a better term I guess ;)) </p>
  
  <p>HTML Code snippet:</p>
  
  <pre><code>&lt;form action="postad.php?cityid=15&amp;subcatid=1" method="post" name="frmPost" enctype="multipart/form-data" 
  
   &lt;!-- Some other code --&gt;
  
  
  &lt;td&gt;&lt;input name="showemail" type="radio" value="1" &gt;&amp;nbsp;&lt;/td&gt;
  &lt;table class="postad" cellspacing="0" cellpadding="0" border="0" width="100%"&gt;
  
  &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;Upload Pictures:&lt;/b&gt;&lt;br&gt;
      &lt;span class="hint"&gt;Maximum filesize: 300KB&lt;/span&gt;&lt;br&gt;
                  &lt;input type="file" name="pic[]" size="69"&gt;&lt;br&gt;
          &lt;img src="images/spacer.gif" height="2"&gt;&lt;br&gt;
                  &lt;input type="file" name="pic[]" size="69"&gt;&lt;br&gt;
          &lt;img src="images/spacer.gif" height="2"&gt;&lt;br&gt;
                  &lt;input type="file" name="pic[]" size="69"&gt;&lt;br&gt;
          &lt;img src="images/spacer.gif" height="2"&gt;&lt;br&gt;
                  &lt;input type="file" name="pic[]" size="69"&gt;&lt;br&gt;
          &lt;img src="images/spacer.gif" height="2"&gt;&lt;br&gt;
                  &lt;input type="file" name="pic[]" size="69"&gt;&lt;br&gt;
          &lt;img src="images/spacer.gif" height="2"&gt;&lt;br&gt;
              &lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;/table&gt;
  
  
  &lt;!-- some other code --&gt;
  &lt;input name="do" type="hidden" id="do" value="post"&gt;
  &lt;button type="submit"&gt;Post Now&lt;/button&gt;
  </code></pre>
  
  <p>So tackle this I am using this code, but Every time I am failing to attach images. Would you guys help me in this regard.</p>
  
  <pre><code>raw_params={"adtitle":"sample title",
          "area":"sample area",
          "addesc":"&lt;p&gt;sample post&lt;/p&gt;",
          "price":"2000",
          "x[1]":"2012",
          "email":"abc@def.com",
          "showemail":"2",
          "subcatid":"15",
      "do":"post",
          }
  
  encoded_params=urllib.urlencode(raw_params)
  target_page = 'http://some_domain_name/admin/postad.php?cityid=15&amp;subcatid=15'
  opener.open(target_page,encoded_params)
  </code></pre>
  
  <p>I forget to tell you one thing this is the log I got from LIVEHTTPHeader (mozilla-plugin)</p>
  
  <pre><code>Content-Disposition: form-data; name="adtitle"
  
  sample title
  -----------------------------20165274802361271281051822614
  Content-Disposition: form-data; name="area"
  
  sample location
  -----------------------------20165274802361271281051822614
  Content-Disposition: form-data; name="addesc"
  
  &lt;p&gt;sample post&lt;/p&gt;
  -----------------------------20165274802361271281051822614
  Content-Disposition: form-data; name="price"
  
  200
  -----------------------------20165274802361271281051822614
  Content-Disposition: form-data; name="x[1]"
  
  2012
  -----------------------------20165274802361271281051822614
  Content-Disposition: form-data; name="email"
  
  abc@def.com
  -----------------------------20165274802361271281051822614
  Content-Disposition: form-data; name="showemail"
  
  2
  -----------------------------20165274802361271281051822614
  Content-Disposition: form-data; name="pic[]"; filename="3_d_flower.jpg"
  Content-Type: image/jpeg
  
  ÿØÿà
  </code></pre>
  
  <p>And please through some light on this:
      filename="3_d_flower.jpg</p>