Python聊天室程序(基礎(chǔ)版)
更新時(shí)間:2018年04月01日 15:33:21 作者:tom555cat
這篇文章主要為大家詳細(xì)介紹了Python聊天室程序的基礎(chǔ)版,包含客戶端和服務(wù)器端兩部分,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(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)聽服務(wù)器的消息采用了多線程的方法。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python實(shí)現(xiàn)多人聊天室
- python socket多線程通訊實(shí)例分析(聊天室)
- 小小聊天室Python代碼實(shí)現(xiàn)
- python實(shí)現(xiàn)簡單多人聊天室
- 基于python實(shí)現(xiàn)聊天室程序
- Python socket實(shí)現(xiàn)簡單聊天室
- python實(shí)現(xiàn)文本界面網(wǎng)絡(luò)聊天室
- python實(shí)現(xiàn)簡單聊天室功能 可以私聊
- python3實(shí)現(xiàn)多線程聊天室
- Python聊天室?guī)Ы缑鎸?shí)現(xiàn)的示例代碼(tkinter,Mysql,Treading,socket)
相關(guān)文章
用pycharm開發(fā)django項(xiàng)目示例代碼
這篇文章主要介紹了用pycharm開發(fā)django項(xiàng)目示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
python 實(shí)現(xiàn)對(duì)文件夾中的圖像連續(xù)重命名方法
今天小編就為大家分享一篇python 實(shí)現(xiàn)對(duì)文件夾中的圖像連續(xù)重命名方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Django 實(shí)現(xiàn)前端圖片壓縮功能的方法
今天小編就為大家分享一篇Django 實(shí)現(xiàn)前端圖片壓縮功能的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
selenium執(zhí)行js并繞過webdriver監(jiān)測常見方法
這篇文章主要為大家介紹了selenium執(zhí)行js并繞過webdriver監(jiān)測常見方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
Python實(shí)現(xiàn)多項(xiàng)式擬合正弦函數(shù)詳情
這篇文章主要介紹了Python實(shí)現(xiàn)多項(xiàng)式擬合正弦函數(shù)詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08

