title: Getting phantomjs, socket.io and gevent-socketio to work together
body: <p>I am trying to build an application that utilizes Phantomjs 1.7 (simulating a browser) and create a Python back-end to fire up some events and collect data. </p>
  
  <p>The problem is that the two processes Phantomjs and my Python program need to communicate bi-bidirectionally. The problem is that inside <code>page.evaluate</code> I cannot:</p>
  
  <ul>
  <li>pass any complex objects such as "fs" (to read from stdin)</li>
  <li>create a WebSocket to connect to my Python script</li>
  <li>any other form of inter-process communication is restricted</li>
  </ul>
  
  <p>So my solution is simple: </p>
  
  <ul>
  <li>Inject socket.io js into the page I am viewing. </li>
  <li>Connect to my python server which is implemented using gevent-socketio</li>
  </ul>
  
  <p>When I try to connect from inside page.evaluate I get:</p>
  
  <pre><code>Unexpected response code: 404
  </code></pre>
  
  <p>Here is the Phantomjs script:</p>
  
  <pre class="lang-js prettyprint-override"><code>var page   = require("webpage").create();
  page.onAlert = function(msg) { console.log("alert&gt;&gt;&gt;" + msg); };
  page.onConsoleMessage= function(msg) { console.log(msg); };
  
  page.open("http://google.com", function() {
    page.injectJs("socket.io.js");
    page.evaluate(function() {
      var socket = new io.Socket();
      socket.connect('localhost:5051/test');
      socket.on('connect',function() {
        console.log('Client has connected to the server!');
      });
      // Add a connect listener
      socket.on('signal',function(data) {
        console.log('Received a signal: ',data);
      });
      // Add a disconnect listener
      socket.on('disconnect',function() {
        console.log('The client has disconnected!');
      });
      // Sends a message to the server via sockets
      socket.send("kakalq");
    });
      //phantom.exit();
  });
  </code></pre>
  
  <p>And here is the server-side Python script:</p>
  
  <pre class="lang-py prettyprint-override"><code>from socketio import socketio_manage
  from socketio.server import SocketIOServer
  from socketio.namespace import BaseNamespace
  
  class MyNs(BaseNamespace):
    def initialize(self):
      print "connected"
      self.emit('connect')
  
    def disconnect(self, *args, **kwargs):
      print "diconnecting"
      super(MyNs, self).disconnect(*args, **kwargs)
  
   def signal(self, message):
     print "received signal", message
     self.emit("okay", "this will be sent to js")
  
   def start(environ, start_response):
     if environ['PATH_INFO'].startswith('/test'):
       return socketio_manage(environ, { '/test': MyNs })
  
  
  if __name__ == "__main__":
    server = SocketIOServer( ('', 5051), start,policy_server=False )
    server.serve_forever()
  </code></pre>