基于進(jìn)程內(nèi)通訊的python聊天室實(shí)現(xiàn)方法
更新時(shí)間:2015年06月28日 15:44:43 作者:不吃皮蛋
這篇文章主要介紹了基于進(jìn)程內(nèi)通訊的python聊天室實(shí)現(xiàn)方法,實(shí)例分析了Python聊天室的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
本文實(shí)例講述了基于進(jìn)程內(nèi)通訊的python聊天室實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:
#!/usr/bin/env python
# Added by <ctang@redhat.com>
import sys
import os
from multiprocessing import connection
ADDR = ('', 9997)
AUTH_KEY = '12345'
class Server(object):
def __init__(self, username):
self.auth_key = AUTH_KEY
self.addr = ADDR
self.username = username
self.listener = connection.Listener(self.addr, authkey=self.auth_key)
def listen(self):
while True:
conn = self.listener.accept()
while True:
try:
request = conn.recv()
response = self.response(request)
conn.send(response)
except EOFError:
break
conn.close()
def reply(self):
message = raw_input("%s: " % self.username)
return message
def output_request(self, request):
sys.stdout.write('%s says: %s\n' % request)
def response(self, request):
self.output_request(request)
response = (self.username, self.reply())
return response
class Client(object):
def __init__(self, username):
self.auth_key = AUTH_KEY
self.addr = ADDR
self.username = username
self.display_name = self.make_display_name(username)
def make_display_name(self, username):
return "%s: " % username
def connect(self):
self.conn = connection.Client(self.addr, authkey=self.auth_key)
while True:
message = raw_input(self.display_name)
self.send(message)
response = self.conn.recv()
self.output_response(response)
def send(self, message):
self.conn.send((self.username, message))
def output_response(self, response):
sys.stdout.write('%s says: %s\n' % response)
def main():
mode = sys.argv[1]
if mode == 'server':
username = raw_input("Your name please: ")
server = Server(username)
server.listen()
elif mode == 'client':
username = raw_input("Your name please: ")
client = Client(username)
client.connect()
if __name__ == '__main__':
main()
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- python實(shí)現(xiàn)多人聊天室
- python編寫簡(jiǎn)易聊天室實(shí)現(xiàn)局域網(wǎng)內(nèi)聊天功能
- Python Socket編程之多線程聊天室
- 基于python實(shí)現(xiàn)聊天室程序
- Python實(shí)現(xiàn)基于C/S架構(gòu)的聊天室功能詳解
- Python聊天室程序(基礎(chǔ)版)
- Python socket實(shí)現(xiàn)簡(jiǎn)單聊天室
- python基于twisted框架編寫簡(jiǎn)單聊天室
- 小小聊天室Python代碼實(shí)現(xiàn)
- python socket多線程通訊實(shí)例分析(聊天室)
- Python聊天室實(shí)例程序分享
- Python實(shí)現(xiàn)的使用telnet登陸聊天室實(shí)例
- Python socket C/S結(jié)構(gòu)的聊天室應(yīng)用實(shí)現(xiàn)
- python實(shí)現(xiàn)簡(jiǎn)單多人聊天室
相關(guān)文章
python利用有道翻譯實(shí)現(xiàn)"語言翻譯器"的功能實(shí)例
小編就為大家分享一篇python利用有道翻譯實(shí)現(xiàn)"語言翻譯器"的功能實(shí)例。具有比較好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-11-11
Python中關(guān)于列表的常規(guī)操作范例以及介紹
列表是一種有序的集合,可以隨時(shí)添加和刪除其中的元素。在python中使用的頻率非常高,本篇文章對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-09-09
對(duì)python3 中方法各種參數(shù)和返回值詳解
今天小編就為大家分享一篇對(duì)python3 中方法各種參數(shù)和返回值詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python使用tarfile模塊實(shí)現(xiàn)免費(fèi)壓縮解壓
Python自帶的tarfile模塊可以方便讀取tar歸檔文件,厲害的是可以處理使用gzip和bz2壓縮歸檔文件tar.gz和tar.bz2,這篇文章主要介紹了Python使用tarfile模塊實(shí)現(xiàn)免費(fèi)壓縮解壓,需要的朋友可以參考下2024-03-03

