91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python聊天室程序(基礎版)

發布時間:2020-09-28 08:26:46 來源:腳本之家 閱讀:161 作者:tom555cat 欄目:開發技術

本文實例為大家分享了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('<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() 

 服務器端代碼:

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

客戶端在監聽服務器的消息采用了多線程的方法。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

宁乡县| 肇州县| 菏泽市| 博兴县| 乌兰察布市| 迁西县| 启东市| 绥化市| 临桂县| 十堰市| 奉化市| 静乐县| 乌鲁木齐市| 涿州市| 常宁市| 肥城市| 大化| 枝江市| 公主岭市| 绥德县| 玉山县| 资讯| 温宿县| 五常市| 上栗县| 普洱| 南京市| 边坝县| 枣强县| 丰顺县| 宁明县| 威远县| 江华| 永仁县| 大埔区| 溧阳市| 资兴市| 宁波市| 白朗县| 尼木县| 永春县|