Advertisement
Guest User

blackbeard

a guest
Feb 1st, 2011
1,774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. import socket
  2. import sys
  3. import threading
  4.  
  5.  
  6. def start():
  7.     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  8.     sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  9.     # listen for upto 50 cnxns on port 8000
  10.     sock.bind(('', 8000))
  11.     sock.listen(50)
  12.  
  13.     while True:
  14.         csock,caddr = sock.accept()
  15.         print "Connection from: ", caddr
  16.         # Start a thread to service each cnxn
  17.         t = threading.Thread(target=handle_cnxn, args=(csock,))
  18.         t.start()
  19.  
  20.  
  21. def handle_cnxn(csock):
  22.     shake1 = csock.recv(1024)
  23.    
  24.     shakelist = shake1.split("\r\n")
  25.     # The body follows a \r\n after the 'headers'
  26.     body = shake1.split("\r\n\r\n")[1]
  27.  
  28.     # Extract key1 and key2
  29.     for elem in shakelist:
  30.         if elem.startswith("Sec-WebSocket-Key1:"):
  31.             key1 = elem[20:]  # Sec-WebSocket-Key1: is 20 chars
  32.         elif elem.startswith("Sec-WebSocket-Key2:"):
  33.             key2 = elem[20:]
  34.         else:
  35.             continue
  36.  
  37.     # Count spaces
  38.     nums1 = key1.count(" ")
  39.     nums2 = key2.count(" ")
  40.     # Join digits in the key
  41.     num1 = ''.join([x for x in key1 if x.isdigit()])
  42.     num2 = ''.join([x for x in key2 if x.isdigit()])
  43.    
  44.     # Divide the digits by the num of spaces
  45.     key1 = int(int(num1)/int(nums1))
  46.     key2 = int(int(num2)/int(nums2))
  47.  
  48.     # Pack into Network byte ordered 32 bit ints
  49.     import struct
  50.     key1 = struct.pack("!I", key1)
  51.     key2 = struct.pack("!I", key2)
  52.  
  53.     # Concat key1, key2, and the the body of the client handshake and take the md5 sum of it
  54.     key = key1 + key2 + body
  55.     import hashlib
  56.     m = hashlib.md5()
  57.     m.update(key)
  58.     d = m.digest()
  59.  
  60.     # Send 'headers'
  61.     csock.send("HTTP/1.1 101 WebSocket Protocol Handshake\r\n")
  62.     csock.send("Upgrade: WebSocket\r\n")
  63.     csock.send("Connection: Upgrade\r\n")
  64.     csock.send("Sec-WebSocket-Origin: http://127.0.0.1:8080\r\n")
  65.     csock.send("Sec-WebSocket-Location: ws://127.0.0.1:8000/\r\n")
  66.     csock.send("Sec-WebSocket-Protocol: chat\r\n")
  67.     csock.send("\r\n")
  68.     #Send digest
  69.     csock.send(d)
  70.  
  71.     # Message framing - 0x00 utf-8-encoded-body 0xFF
  72.     def send(data):
  73.         first_byte = chr(0x00)
  74.         payload = data.encode('utf-8')
  75.         pl = first_byte + payload + chr(0xFF)
  76.         csock.send(pl)
  77.  
  78.    
  79.     from time import sleep
  80.  
  81.     # This is dependent on you - what you wish to send to the browser
  82.     i = 0
  83.     while True:
  84.         send(u"%s" % (i))
  85.         i += 1
  86.         sleep(1)
  87.  
  88. if __name__ == "__main__":
  89.     start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement