玩蛇网提供最新Python编程技术信息以及Python资源下载!

在线聊天室python 服务器端代码片段

python 培训

以下python代码是基于Html5 websocket和Python的在线聊天室,python 服务器端代码片段。完整代码还在搜集中........

需要使用到的python模块有:socket、struct、hashlib、threading、python random

import socket
import struct
import hashlib
import threading,random

connectionlist = {}

def sendMessage(message):
	global connectionlist
	for connection in connectionlist.values():
		connection.send("\x00%s\xFF" % message)

def deleteconnection(item):
	global connectionlist
	del connectionlist['connection'+item]
	
class WebSocket(threading.Thread):
	def __init__(self,conn,index,name,remote, path="/"):
		threading.Thread.__init__(self)
		self.conn = conn
		self.index = index
		self.name = name
		self.remote = remote
		self.path = path
		self.buffer = ""
		
	def run(self):
		print 'Socket%s Start!' % self.index
		headers = {}
		self.handshaken = False

		while True:
			if self.handshaken == False:
				print 'Socket%s Start Handshaken with %s!' % (self.index,self.remote)
				self.buffer += self.conn.recv(1024)
				if self.buffer.find('\r\n\r\n') != -1:
					header, data = self.buffer.split('\r\n\r\n', 1)
					for line in header.split("\r\n")[1:]:
						key, value = line.split(": ", 1)
						headers[key] = value

					headers["Location"] = "ws://%s%s" %(headers["Host"], self.path)
					key1 = headers["Sec-WebSocket-Key1"]
					key2 = headers["Sec-WebSocket-Key2"]
					if len(data) < 8:
						data += self.conn.recv(8-len(data))
					key3 = data[:8]
					self.buffer = data[8:]
					token = self.generate_token(key1, key2, key3)
					
					handshake = '\
HTTP/1.1 101 Web Socket Protocol Handshake\r\n\
Upgrade: WebSocket\r\n\
Connection: Upgrade\r\n\
Sec-WebSocket-Origin: %s\r\n\
Sec-WebSocket-Location: %s\r\n\r\n\
' %(headers['Origin'], headers['Location'])


#www.iplaypython.com
					self.conn.send(handshake+token)
					self.handshaken = True
					print 'Socket%s Handshaken with %s success!' % (self.index,self.remote)
					sendMessage('Welcome, '+self.name+' !')
			else:
				self.buffer += self.conn.recv(64)
				if self.buffer.find("\xFF")!=-1:
					s = self.buffer.split("\xFF")[0][1:]
					if s=='quit':
						print 'Socket%s Logout!' % (self.index)
						sendMessage(self.name+' Logout')
						deleteconnection(str(self.index))
						self.conn.close()
						break
					else:
						print 'Socket%s Got msg:%s from %s!' % (self.index,s,self.remote)
						sendMessage(self.name+':'+s)
					self.buffer = ""
	
	def generate_token(self, key1, key2, key3):
		num1 = int("".join([digit for digit in list(key1) if digit.isdigit()]))
		spaces1 = len([char for char in list(key1) if char == " "])
		num2 = int("".join([digit for digit in list(key2) if digit.isdigit()]))
		spaces2 = len([char for char in list(key2) if char == " "])

		combined = struct.pack(">II", num1/spaces1, num2/spaces2) + key3
		return hashlib.md5(combined).digest()
	
		

class WebSocketServer(object):
	def __init__(self):
		self.socket = None
	def begin(self):
		print 'WebSocketServer Start!'
		self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.socket.bind(("192.168.20.31",1234))
		self.socket.listen(50)
		
		global connectionlist
		
		i=0
		while True:
			connection, address = self.socket.accept()
			
			username=address[0]
			
			newSocket = WebSocket(connection,i,username,address)
			newSocket.start()
			connectionlist['connection'+str(i)]=connection
			i = i + 1

if __name__ == "__main__":
	server = WebSocketServer()
	server.begin()
			

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/other/o2406.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-04-13 16:04 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部