手机
当前位置:查字典教程网 >脚本专栏 >python >python聊天程序实例代码分享
python聊天程序实例代码分享
摘要:代码简单,直接看代码吧:复制代码代码如下:importsocketimportthreadingimportre#importTkinter...

代码简单,直接看代码吧:

复制代码 代码如下:

import socket

import threading

import re

#import Tkinter

def ser():

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.bind(('',33333))

s.listen(1)

conn,addr=s.accept()

while True:

print '[%s:%d] send a message to me: %s'%(addr[0],addr[1],conn.recv(1024))

s.close()

def clt():

c=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

ip_pattern=re.compile(r'^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$')

while True:

ip=raw_input('Input the Server's IPv4 address:')

ip_match=ip_pattern.match(ip)

if ip_match:

break

c.connect((ip,33333))

while True:

sms=raw_input('Input the message you want to send:')

c.sendall(sms)

c.close()

if __name__=="__main__":

ser=threading.Thread(target=ser)

clt=threading.Thread(target=clt)

ser.start()

clt.start()

ser.join()

clt.join()

【python聊天程序实例代码分享】相关文章:

Python多线程学习资料

python利用elaphe制作二维条形码实现代码

python 从远程服务器下载东西的代码

python 中文字符串的处理实现代码

python 多线程应用介绍

python判断端口是否打开的实现代码

动态创建类实例代码

python发送邮件的实例代码(支持html、图片、附件)

Python 时间处理datetime实例

Python常见文件操作的函数示例代码

精品推荐
分类导航