title: Android HttpPost not working with python CGI server
body: <p>I'm buliding an android app and need to submit data to my python CGI server. I'm usually pulling data off the server with HttpGet and all those calls work correctly and quickly. When I try to push data with HttpPost, the program hangs indefinitely until I long press the return button on the emulator and force quit the program. The CGI server seems to start the script but doesn't return until I force quit the android app. When the CGI script returns, it says <code>CGI script exited OK</code> but it hasn't done anything.</p>
  
  <p>At the moment I'm executing the request in the UI thread. I know it should be done in an AsyncTask but I want a solution before a try a good solution.</p>
  
  <p>I've spent 3 days trying different things and looking at forums with no luck. I'd really appreciate suggestions. Here's the relevant part of my Android code:</p>
  
  <pre><code>private final String serverIP = "10.0.2.2";
  
  HttpClient httpclient = new DefaultHttpClient();
  HttpParams params = httpclient.getParams();
  HttpConnectionParams.setConnectionTimeout(params, 5000);
  HttpConnectionParams.setSoTimeout(params, 5000);
  String URI = "http://"+serverIP+":8000/cgi-bin/test.py?order_submit=0";
  HttpPost post = new HttpPost(URI);
  
  List&lt;NameValuePair&gt; kvPairs = new ArrayList&lt;NameValuePair&gt;(2);  
  kvPairs.add(new BasicNameValuePair("name", "bob"));
  kvPairs.add(new BasicNameValuePair("surname", "the builder"));
  
  try {
      Log.i(TAG, "Trying to set Entity");
      post.setEntity(new UrlEncodedFormEntity(kvPairs, HTTP.UTF_8));
  
      Log.i(TAG, "Trying to Post");
  
      HttpResponse response = httpclient.execute(post);
  
      Log.i(TAG, "execute done");
      httpclient.getConnectionManager().closeExpiredConnections();
  
  } catch (ClientProtocolException e) {
      Log.e(TAG,e.toString());
  } catch (UnsupportedEncodingException e) {
      Log.e(TAG,e.toString());
  } catch (IOException e) {
      Log.e(TAG,e.toString());
  }
  </code></pre>
  
  <p>which outputs the following in LogCat:</p>
  
  <p><code>INFO/App:(534): Trying to set Entity</code><br>
  <code>INFO/App:(534): Trying to Post</code><br>
  <code>ERROR/App:(534): java.net.SocketTimeoutException: The operation timed out</code>  </p>
  
  <p>My Python CGI server script is as follows:</p>
  
  <pre><code>import os, sys, cgi, csv
  import cgitb #CGI error backtracer
  cgitb.enable()
  
  form = cgi.FieldStorage()
  
  if "order_submit" in form:    
      ofile = open(os.getcwd() + "/Forms/Output/foo.csv", "wb")
      writer = csv.writer(ofile, delimiter=',', quotechar='"',quoting=csv.QUOTE_ALL)
      writer.writerow(["Name", form["name"].value])
      writer.writerow(["Surname", form["surname"].value])
      ofile.close()
  </code></pre>
  
  <p>After the Android app request has timed out, foo.csv still does not exist. The same is true even if the <strong>if</strong> part of the script contains the following, and doesn't use the CGI form at all:</p>
  
  <pre><code>if "order_submit" in form:    
      ofile = open(os.getcwd() + "/Forms/Output/foo.csv", "wb")
      writer = csv.writer(ofile, delimiter=',', quotechar='"',quoting=csv.QUOTE_ALL)
      writer.writerow(["Name", "harry"])
      ofile.close()  
  </code></pre>
  
  <p>After the <code>httpclient.execute(post);</code> , this is the order of events:<br>
  App: <code>httpclient.execute(post);</code><br>
  CGI server: <code>POST /cgi-bin/test.py?order_submit=0 HTTP/1.1" 200</code><br>
  CGI server: starts the 2nd python script<br>
  <em>a few second's waiting...</em><br>
  App: <code>java.net.SocketTimeoutException: The operation timed out</code><br>
  Python Script: creates the correct dummy file<br>
  CGI server: <code>CGI script exited OK</code>  </p>