-
[原创]python最简单的聊天法度
添加时间:2013-5-28 点击量:写个聊天法度,开两个线程,便是客户端,也是办事器。
#Writen by Hymenz
import socket
import threading
import re
#import Tkinter
def serv():
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
pt=3333
s.bind((,pt))
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()
thread.exit()
def clt():
c=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip_pattern=re.compile(r^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|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,3333))
while True:
sms=raw_input(Input the message you want to send:)
c.sendall(sms)
c.close()
sth=threading.Thread(target=serv)
cth=threading.Thread(target=clt)
sth.start()
cth.start()
真正的心灵世界会告诉你根本看不见的东西,这东西需要你付出思想和灵魂的劳动去获取,然后它会照亮你的生命,永远照亮你的生命。——王安忆《小说家的十三堂课》