资源简介
python多人聊天服务器,供新人学习socket用,适合flash,actionscript
![](http://www.nz998.com/pic/48737.jpg)
代码片段和文件信息
#!/usr/bin/python
#-*- coding: utf-8 -*-
import socket
host = “127.0.0.1“
port = 9921
addr = hostport
clients = {}
def stringPolishing(value):
length = 6-len(value)
for i in range(length):
value = ‘0‘+value
return value
def decoderData(valuesock):
position = 0
print value
try:
commandLen = int(value[0:6])
position += 6
command = value[position:position+commandLen]
print ‘command‘command
position += commandLen
if command==“login“:
nameLen = int(value[position:position+6])
position += 6
userName = value[position:position+nameLen]
print ‘userName‘+userName
sendData = ‘‘
for key in clients.keys():
sendData += stringPolishing(str(len(key)))
sendData += key
clients[key].send(‘000005login‘+stringPolishing(str(nameLen))+userName)
sock.send(‘000010currPeople‘+stringPolishing(str(len(clients)))+sendData)
clients[userName] = sock
elif command==“talk“:
for key in clients.keys():
clients[key].send(value)
elif command==“logout“:
nameLen = int(value[position:position+6])
position += 6
userName = value[position:position+nameLen]
clients[userName].close()
del clients[userName]
for key in clients.keys():
clients[key].send(value)
except ValueError:
sock.send(“command error“)
server = socket.socket(socket.AF_INETsocket.SOCK_STREAM)
server.bind(addr)
server.listen(999)
while True:
clientSockaddrs = server.accept()
print “client ip:“ + str(addrs[0])
print “client port:“ + str(addrs[1])
clientData = clientSock.recv(2000)
decoderData(clientDataclientSock)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 51962 2010-11-25 18:33 聊天程序协议.pdf
文件 647 2010-11-03 21:40 policyServer.py
文件 1927 2010-11-25 20:13 ChatServer.py
----------- --------- ---------- ----- ----
54536 3
- 上一篇:excel转化成lua 表
- 下一篇:RGSS3A解包器Python版
相关资源
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
- 量化交易(附python常见函数的使用方
- python 名字用字排行
评论
共有 条评论