Python聊天室程序(基础版)

yipeiwu_com5年前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('<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() 

客户端在监听服务器的消息采用了多线程的方法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python 实现12306登录功能实例代码

下面一段代码给大家带来了python实现12306登录功能,具体代码如下所示: #!/usr/bin/env python import requests import urllib...

在python中用print()输出多个格式化参数的方法

不废话,直接贴代码: disroot = math.sqrt(deta) root1 = (-b + disroot)/(2*a) root2 = (-b - disroot)/(2...

Python基础入门之seed()方法的使用

 seed() 设置生成随机数用的整数起始值。调用任何其他random模块函数之前调用这个函数。 语法 以下是seed()方法的语法: seed ( [x] ) 注意...

python区分不同数据类型的方法

python区分不同数据类型的方法

python怎么区分不同数据类型? Python判断变量的数据类型的两种方法 一、Python中的数据类型有数字、字符串,列表、元组、字典、集合等。有两种方法判断一个变量的数据类型 1、...

安装Python的web.py框架并从hello world开始编程

最近有一个小的web项目,想用喜爱都python,但是想到之前接触过都django我感觉一阵不寒而栗,为什么?Django的配置太过复杂,而且小项目不太适合MVC的开发模式,所以...