0

New to python and websockets but I am trying to do the following:

import httplib
conn = httplib.HTTPConnection('localhost:8888')

conn.putheader('Connection', 'Upgrade')
conn.putheader('Upgrade', 'websocket')
conn.endheaders()
conn.send('Hello to websocket!')

Crashes when it reaches putheader

Server code is written in Node.js

var http = require('http');

http.createServer(function (request, response) {
    console.log(request);
}).on('upgrade', function(request, socket, head) {
    console.log('Upgrade request! Woohoo!');
    socket.connect(8080, 'localhost', function(data) {
        console.log('data');
    });
    socket.write('Socket is open!');
}).listen(8888);

Not really worried about the server code being correct (I'll fix it if need be once I get a request sent from my Python client), but I am wondering how to upgrade a connection to websocket using httplib (if it is possible).

moesef
  • 4,641
  • 16
  • 51
  • 68

2 Answers2

0

I'm not sure, but httplib has a problem with asynchronous requests. Look framework tornado, he can work with websocket and asynchronous requests and very simple.

i.krivosheev
  • 387
  • 3
  • 18
0

httplib does not speak WebSocket. Adding an upgrade header manually does not magically make it so. It is also working synchronously, which for WebSocket, is undesirable.

Apart from Tornado already mentioned, you might also look at Autobahn, which can use both Twisted and asyncio as it's underlying asynchronous network library.

Disclosure: I am original author of Autobahn and work for Tavendo.

oberstet
  • 21,353
  • 10
  • 64
  • 97
  • i see. I will checkout out the projects you have provided, but must ask if there is a way to create a socket from an HTTPConnection object? or are httplib and sockets complete seperate modules? – moesef Mar 29 '14 at 08:27
  • Not sure if I get what you mean: WebSocket is not just "raw TCP sockets". So even if you can get at the raw TCP socket underlying a HTTPConnection, that wouldn't help much .. – oberstet Mar 29 '14 at 09:09
  • I see. I probably need to read up on the tech a little more. Like I said, first time messing with this stuff :P – moesef Mar 30 '14 at 08:01