Python socket實(shí)現(xiàn)簡(jiǎn)單聊天室
本文實(shí)例為大家分享了Python socket實(shí)現(xiàn)簡(jiǎn)單聊天室的具體代碼,供大家參考,具體內(nèi)容如下
服務(wù)端使用了select模塊,實(shí)現(xiàn)了對(duì)多個(gè)socket的監(jiān)控??蛻舳擞捎趕elect在Windows下只能對(duì)socket使用,所以使用了多線程來(lái)實(shí)現(xiàn)對(duì)客戶端輸入和socket連接的同時(shí)監(jiān)控。注意這里的socket設(shè)置為了非阻塞。這樣就實(shí)現(xiàn)了在一個(gè)線程中同時(shí)進(jìn)行socket的接收和發(fā)送。
服務(wù)器代碼:
# -*- coding: utf-8 -*-
import socket,select
connection_list = []
host = ''
port = 10001
def board_cast(sock,message):
for socket in connection_list:
if socket != server_sock and socket != sock:
try:
socket.send(message)
except:
socket.close()
print str(socket.getpeername())+' is offline'
connection_list.remove(socket)
server_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
server_sock.setblocking(0)
server_sock.bind((host,port))
server_sock.listen(10)
connection_list.append(server_sock)
while 1:
readable,writable,error = select.select(connection_list,[],[])
for sock in readable:
if sock == server_sock:
connection,connection_add = sock.accept()
message = str(connection_add)+'enter room'
board_cast(connection,message)
print connection_add,'%s connect'
connection_list.append(connection)
else:
try:
date = sock.recv(1024)
print date
board_cast(sock,'('+str(sock.getpeername())+') :'+date)
except:
message2 = str(sock.getpeername())+ 'is offline'
board_cast(sock,message2)
print str(sock.getpeername())+ ' is offline'
sock.close()
connection_list.remove(sock)
continue
客戶端代碼:
# -*- coding: utf-8 -*-
import socket,threading,time
flag = 0
date = ''
lock = threading.Lock()
host = 'localhost'
port = 10001
client_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client_sock.setblocking(0)
class Mythread1(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global flag, date
while 1:
date = raw_input()
if len(date):
lock.acquire()
flag = 1
lock.release()
class Mythread2(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global flag
global date
while 1:
try:
buf = client_sock.recv(1024)
if len(buf):
print buf
except:
pass
if flag:
try:
client_sock.send(date)
except socket.error, e:
print e
lock.acquire()
flag = 0
lock.release()
try:
client_sock.connect((host,port))
print"連接成功"
except socket.error,e:
print e
t1 = Mythread1()
t2 = Mythread2()
t1.start()
t2.start()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python socket多線程通訊實(shí)例分析(聊天室)
- Python聊天室?guī)Ы缑鎸?shí)現(xiàn)的示例代碼(tkinter,Mysql,Treading,socket)
- python socket 聊天室實(shí)例代碼詳解
- Python Socket編程之多線程聊天室
- python socket實(shí)現(xiàn)聊天室
- Python基于Socket實(shí)現(xiàn)簡(jiǎn)單聊天室
- 基于Python socket實(shí)現(xiàn)簡(jiǎn)易網(wǎng)絡(luò)聊天室
- python使用socket制作聊天室詳細(xì)源碼(可以直接運(yùn)行)
相關(guān)文章
局域網(wǎng)內(nèi)python socket實(shí)現(xiàn)windows與linux間的消息傳送
這篇文章主要介紹了局域網(wǎng)內(nèi)python socket實(shí)現(xiàn)windows與linux間的消息傳送的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
Python代碼統(tǒng)計(jì)耗時(shí)的方法詳解
在現(xiàn)代軟件開(kāi)發(fā)中,性能優(yōu)化是一個(gè)至關(guān)重要的環(huán)節(jié),無(wú)論是開(kāi)發(fā)大型系統(tǒng)還是小型工具,開(kāi)發(fā)者都需要對(duì)代碼的執(zhí)行時(shí)間進(jìn)行精確測(cè)量,以便找出瓶頸并優(yōu)化性能,本文給大家介紹了Python代碼統(tǒng)計(jì)耗時(shí)的方法,需要的朋友可以參考下2025-02-02
Python aiohttp百萬(wàn)并發(fā)極限測(cè)試實(shí)例分析
這篇文章主要介紹了Python aiohttp百萬(wàn)并發(fā)極限測(cè)試,結(jié)合實(shí)例形式分析了Python異步編程基于aiohttp客戶端高并發(fā)請(qǐng)求的相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2019-10-10
Python按行讀取文件的實(shí)現(xiàn)方法【小文件和大文件讀取】
這篇文章主要介紹了Python按行讀取文件的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了針對(duì)小文件和大文件的讀取方法,需要的朋友可以參考下2016-09-09
python3+pyqt5+itchat微信定時(shí)發(fā)送消息的方法
今天小編就為大家分享一篇python3+pyqt5+itchat微信定時(shí)發(fā)送消息的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python中xml和json格式相互轉(zhuǎn)換操作示例
這篇文章主要介紹了Python中xml和json格式相互轉(zhuǎn)換操作,結(jié)合實(shí)例形式分析了xmltodict庫(kù)的安裝及xml格式與json格式數(shù)據(jù)相互轉(zhuǎn)換操作技巧,需要的朋友可以參考下2018-12-12
Eclipse和PyDev搭建完美Python開(kāi)發(fā)環(huán)境教程(Windows篇)
這篇文章主要介紹了Eclipse和PyDev搭建完美Python開(kāi)發(fā)環(huán)境教程(Windows篇),具有一定的參考價(jià)值,感興趣的小伙伴可以了解一下。2016-11-11

