最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python實(shí)現(xiàn)決策樹ID3算法的示例代碼

    python實(shí)現(xiàn)決策樹ID3算法的示例代碼

    這篇文章主要介紹了python實(shí)現(xiàn)決策樹ID3算法的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • Python畫圖常用代碼總結(jié)大全(20個(gè)畫圖代碼現(xiàn)拿現(xiàn)用)

    Python畫圖常用代碼總結(jié)大全(20個(gè)畫圖代碼現(xiàn)拿現(xiàn)用)

    Python是一種高級編程語言,擁有豐富的圖形庫,可以完成繪制各種類型的圖形任務(wù),下面這篇文章主要給大家介紹了關(guān)于Python畫圖常用代碼的相關(guān)資料,文中介紹的這20個(gè)畫圖代碼可以現(xiàn)拿現(xiàn)用,需要的朋友可以參考下
    2023-06-06
  • python dict.get()和dict[''key'']的區(qū)別詳解

    python dict.get()和dict[''key'']的區(qū)別詳解

    下面小編就為大家?guī)硪黄猵ython dict.get()和dict['key']的區(qū)別詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06
  • Python數(shù)學(xué)建模學(xué)習(xí)模擬退火算法約束條件處理示例解析

    Python數(shù)學(xué)建模學(xué)習(xí)模擬退火算法約束條件處理示例解析

    線性規(guī)劃(Linear programming),是研究線性約束條件下線性目標(biāo)函數(shù)的極值問題的優(yōu)化方法,常用于解決利用現(xiàn)有的資源得到最優(yōu)決策的問題,本文使用懲罰函數(shù)法,分析模擬退火算法處理線性規(guī)劃問題,相關(guān)內(nèi)容也適用于非線性規(guī)劃問題
    2021-10-10
  • python 用所有標(biāo)點(diǎn)符號分隔句子的示例

    python 用所有標(biāo)點(diǎn)符號分隔句子的示例

    今天小編就為大家分享一篇python 用所有標(biāo)點(diǎn)符號分隔句子的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python爬蟲爬取股票的北上資金持倉數(shù)據(jù)

    python爬蟲爬取股票的北上資金持倉數(shù)據(jù)

    這篇文章主要介紹了python爬蟲爬取股票的北上資金持倉數(shù)據(jù),文章基于python的相關(guān)資料展開爬取數(shù)據(jù)的詳細(xì)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • 在Pycharm中調(diào)試Django項(xiàng)目程序的操作方法

    在Pycharm中調(diào)試Django項(xiàng)目程序的操作方法

    今天小編就為大家分享一篇在Pycharm中調(diào)試Django項(xiàng)目程序的操作方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python的類成員變量默認(rèn)初始值的坑及解決

    Python的類成員變量默認(rèn)初始值的坑及解決

    這篇文章主要介紹了Python的類成員變量默認(rèn)初始值的坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 通過Python 接口使用OpenCV的方法

    通過Python 接口使用OpenCV的方法

    下面小編就為大家分享一篇通過Python 接口使用OpenCV的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • matplotlib 范圍選區(qū)(SpanSelector)的使用

    matplotlib 范圍選區(qū)(SpanSelector)的使用

    這篇文章主要介紹了matplotlib 范圍選區(qū)(SpanSelector)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02

最新評論

石首市| 彰武县| 鄂州市| 翁牛特旗| 沁源县| 抚州市| 阿尔山市| 东山县| 济源市| 开阳县| 积石山| 张家港市| 淅川县| 桐柏县| 彭泽县| 石台县| 淮滨县| 汝南县| 阿城市| 卢湾区| 娱乐| 法库县| 北安市| 诏安县| 怀仁县| 西贡区| 普兰县| 都匀市| 大渡口区| 茌平县| 固安县| 桃园县| 天门市| 黑龙江省| 楚雄市| 邢台市| 桂东县| 综艺| 青河县| 厦门市| 澳门|