Python聊天室程序(基礎(chǔ)版)
本文實(shí)例為大家分享了Python聊天室程序的具體代碼,供大家參考,具體內(nèi)容如下
客戶端代碼:
# Filename: socketClient.py
import socket
import sys
import threading
# Client GUI
from tkinter import *
import Pmw
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print (sys.stderr, 'connecting to %s port %s' % server_address)
sock.connect(server_address)
root = Tk()
# textDisplay
textDisplay = Pmw.ScrolledText(root)
textDisplay.pack(expand=1, padx=5, pady=5,side = LEFT)
# textInput
textInput = Pmw.ScrolledText(root)
textInput.pack(expand=1, padx=5, pady=5,side = LEFT)
# Send Button and its callback
def sendMsg(event):
message = socket.gethostname()+':'+ textInput.get()
#print (sys.stderr, 'sending "%s"' % message)
print(message)
sock.sendall(message.encode())
textInput.clear()
#data = sock.recv(100)
#textDisplay.insert(END, data)
#print (sys.stderr, 'received "%s"' % data)
sendBtn = Button(root, text="Send")
sendBtn.bind('<Button-1>', sendMsg)
sendBtn.pack(side = LEFT)
def receiveMsg():
while True:
data = sock.recv(100)
print (sys.stderr, 'client received "%s"' % data)
textDisplay.insert(END, data)
receiveThread = threading.Thread(name='waitForMSG', target=receiveMsg)
receiveThread.start()
root.mainloop()
服務(wù)器端代碼:
# Filename: socketServer.py
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 10000)
print (sys.stderr, 'starting up on %s port %s' % server_address)
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
while True:
# Wait for a connection
print (sys.stderr, 'waiting for a connection')
connection, client_address = sock.accept()
try:
print (sys.stderr, 'connection from', client_address)
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(16)
print (sys.stderr, 'received "%s"' % data)
if data:
print (sys.stderr, 'sending data back to the client')
connection.sendall(data)
else:
print (sys.stderr, 'no data from', client_address)
break
finally:
# Clean up the connection
connection.close()
客戶端在監(jiān)聽(tīng)服務(wù)器的消息采用了多線程的方法。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)多人聊天室
- python socket多線程通訊實(shí)例分析(聊天室)
- 小小聊天室Python代碼實(shí)現(xiàn)
- python實(shí)現(xiàn)簡(jiǎn)單多人聊天室
- 基于python實(shí)現(xiàn)聊天室程序
- Python socket實(shí)現(xiàn)簡(jiǎn)單聊天室
- python實(shí)現(xiàn)文本界面網(wǎng)絡(luò)聊天室
- python實(shí)現(xiàn)簡(jiǎn)單聊天室功能 可以私聊
- python3實(shí)現(xiàn)多線程聊天室
- Python聊天室?guī)Ы缑鎸?shí)現(xiàn)的示例代碼(tkinter,Mysql,Treading,socket)
相關(guān)文章
python實(shí)現(xiàn)決策樹(shù)ID3算法的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)決策樹(shù)ID3算法的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Python畫(huà)圖常用代碼總結(jié)大全(20個(gè)畫(huà)圖代碼現(xiàn)拿現(xiàn)用)
Python是一種高級(jí)編程語(yǔ)言,擁有豐富的圖形庫(kù),可以完成繪制各種類型的圖形任務(wù),下面這篇文章主要給大家介紹了關(guān)于Python畫(huà)圖常用代碼的相關(guān)資料,文中介紹的這20個(gè)畫(huà)圖代碼可以現(xiàn)拿現(xiàn)用,需要的朋友可以參考下2023-06-06
python dict.get()和dict[''key'']的區(qū)別詳解
下面小編就為大家?guī)?lái)一篇python dict.get()和dict['key']的區(qū)別詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
Python數(shù)學(xué)建模學(xué)習(xí)模擬退火算法約束條件處理示例解析
線性規(guī)劃(Linear programming),是研究線性約束條件下線性目標(biāo)函數(shù)的極值問(wèn)題的優(yōu)化方法,常用于解決利用現(xiàn)有的資源得到最優(yōu)決策的問(wèn)題,本文使用懲罰函數(shù)法,分析模擬退火算法處理線性規(guī)劃問(wèn)題,相關(guān)內(nèi)容也適用于非線性規(guī)劃問(wèn)題2021-10-10
python 用所有標(biāo)點(diǎn)符號(hào)分隔句子的示例
今天小編就為大家分享一篇python 用所有標(biāo)點(diǎn)符號(hào)分隔句子的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
python爬蟲(chóng)爬取股票的北上資金持倉(cāng)數(shù)據(jù)
這篇文章主要介紹了python爬蟲(chóng)爬取股票的北上資金持倉(cāng)數(shù)據(jù),文章基于python的相關(guān)資料展開(kāi)爬取數(shù)據(jù)的詳細(xì)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05
在Pycharm中調(diào)試Django項(xiàng)目程序的操作方法
今天小編就為大家分享一篇在Pycharm中調(diào)試Django項(xiàng)目程序的操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
matplotlib 范圍選區(qū)(SpanSelector)的使用
這篇文章主要介紹了matplotlib 范圍選區(qū)(SpanSelector)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

