Source

uwsgi / websockets.py

import uwsgi
import string
import hashlib
import struct

def to_ws_num(value):
	num = ''
	div = 0

	for c in value:
		if c in string.digits:
			num += c
		elif c == ' ':
			div += 1

	return int(num)/div

def application(e, start_response):
	print e


	client = e['wsgi.input'].fileno()

	print client

	data = uwsgi.recv_block(client, 8)

	print "data", data, len(data)

	key1 = to_ws_num(e['HTTP_SEC_WEBSOCKET_KEY1'])
	key2 = to_ws_num(e['HTTP_SEC_WEBSOCKET_KEY2'])

	response = hashlib.md5( struct.pack('>II', key1, key2) + data).digest()

	
	print response
	
	start_response('101 WebSocket Protocol Handshake',[
		('Upgrade', 'WebSocket'),
		('Connection', 'Upgrade'),
		('Sec-WebSocket-Origin', e.get('HTTP_ORIGIN')),
		('Sec-WebSocket-Location','ws://%s%s%s' % (e.get('HTTP_HOST'), e.get('SCRIPT_NAME'), e.get('PATH_INFO')) ),
		('Sec-WebSocket-Protocol', e.get('HTTP_SEC_WEBSOCKET_PROTOCOL', 'default'))
		])

	yield response

	message = uwsgi.recv_frame(client, '\x00', '\xff')
	while message:
		print message
		uwsgi.signal(-17)
		yield '\x00' + message + '\xff'
		if len(message) == 0:
			raise StopIteration
		message = uwsgi.recv_frame(client, '\x00', '\xff')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.