python實現(xiàn)文本界面網(wǎng)絡(luò)聊天室
Hello大家好,今天說一下python的socket編程,基于python的socket通信的文本框網(wǎng)絡(luò)聊天
首先,實驗環(huán)境:
一個云服務(wù)器(我們這里是用的阿里云,大家將就自己的條件吧);
類Unix操作系統(tǒng)(如Mac OS,Linux等);
Windows系列操作系統(tǒng)。
在這里,我使用的是阿里云,Mac OSX,Windows XP(在mac上的一個虛擬機)。
Server.py
# -*- coding: utf-8 -*- #!/usr/local/bin/python import socket import sys import threading con = threading.Condition() HOST = "云空間的IP地址" PORT = 端口 data = '' s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print 'Socket created' s.bind((HOST, PORT)) s.listen(10) print 'Socket now listening' def clientThreadIn(conn, nick):#開辟線程 global data while True:#接受客戶端數(shù)據(jù) try: temp = conn.recv(1024) if not temp: conn.close()#連接關(guān)閉 return NotifyAll(temp) print data except: NotifyAll(nick + " leaves the room!") print data return def NotifyAll(sss):#廣播 global data if con.acquire(): data = sss con.notifyAll() con.release() def ClientThreadOut(conn, nick):#客戶端輸出 global data while True: if con.acquire(): con.wait() if data: try: conn.send(data) con.release() except: con.release() return while 1: conn, addr = s.accept() print 'Connected with ' + addr[0] + ':' + str(addr[1]) nick = conn.recv(1024) NotifyAll('Welcome ' + nick + ' to the room!') print data print str((threading.activeCount() + 1) / 2) + ' person(s)!' conn.send(data) threading.Thread(target = clientThreadIn , args = (conn, nick)).start()#開辟線程 threading.Thread(target = ClientThreadOut , args = (conn, nick)).start() s.close()
Client.py
# -*- coding: utf-8 -*- import socket import threading import getpass inString = '' outString = '' nick = '' def DealOut(s): computername=socket.gethostname()#獲取計算機名 global nick, outString while True: outString = raw_input(nick+":") outString = nick + "@" + computername + ': ' + outString s.send(outString) def DealIn(s): global inString while True: try: inString = s.recv(1024) if not inString: break if outString != inString: print inString except: break nick = getpass.getuser()#獲取操作系統(tǒng)用戶名 ip = "云空間IP地址" sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, 端口)) sock.send(nick) thin = threading.Thread(target = DealIn, args = (sock,))#開辟一個讀入的線程 thin.start() thout = threading.Thread(target = DealOut, args = (sock,))#開辟一個寫出的線程 thout.start()
將server.py上傳云端,運行,如圖:
將client.py在mac系統(tǒng)上運行,如圖:
將client.py在WindowsXP虛擬機上運行,如圖:
OK,這就可以了,一個基于python的socket通信的文本框網(wǎng)絡(luò)聊天室就寫好了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談python數(shù)據(jù)結(jié)構(gòu)之動態(tài)規(guī)劃
這篇文章主要介紹了淺談python數(shù)據(jù)結(jié)構(gòu)之動態(tài)規(guī)劃,可能很多小伙伴會覺得這個詞很陌生,覺得這是一種很復(fù)雜的思想,學(xué)習(xí)起來很困難,其實并不是這樣,動態(tài)規(guī)劃所講述的知識與動態(tài)與規(guī)劃并無太大關(guān)聯(lián),需要的朋友可以參考下2023-07-07Django基礎(chǔ)知識 web框架的本質(zhì)詳解
這篇文章主要介紹了Django基礎(chǔ)知識 web框架的本質(zhì)詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07Python中Matplotlib圖像添加標簽的方法實現(xiàn)
本文主要介紹了Python中Matplotlib圖像添加標簽的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Python+Selenium+Pytesseract實現(xiàn)圖片驗證碼識別
這篇文章主要介紹了利用Python+Selenium+Pytesseract實現(xiàn)圖片驗證碼識別,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-01-01Python常用模塊之threading和Thread模塊及線程通信
這篇文章主要介紹了Python常用模塊之threading和Thread模塊及線程通信,文章為圍繞主題的相關(guān)內(nèi)容展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友看可以參考一下方法2022-06-06Python數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表詳解
循環(huán)鏈表 (Circular Linked List) 是鏈式存儲結(jié)構(gòu)的另一種形式,它將鏈表中最后一個結(jié)點的指針指向鏈表的頭結(jié)點,使整個鏈表頭尾相接形成一個環(huán)形,使鏈表的操作更加方便靈活。本文將詳細介紹一下循環(huán)鏈表的相關(guān)知識,需要的可以參考一下2022-01-01