189 8069 5689

Python聊天室程序(基础版)-创新互联

本文实例为大家分享了Python聊天室程序的具体代码,供大家参考,具体内容如下

创新互联是一家集网站建设,桥西企业网站建设,桥西品牌网站建设,网站定制,桥西网站建设报价,网络营销,网络优化,桥西网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

客户端代码:

# 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('', 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() 

文章名称:Python聊天室程序(基础版)-创新互联
链接地址:http://cdxtjz.cn/article/dcpjsh.html

其他资讯