python核心编程:基于TCP的简易qq聊天室

#coding=utf-8
#客户端代码
from socket import *
#创建套接字
tcpSocket = socket(AF_INET,SOCK_STREAM)
addr = ("192.168.123.1",7878)
#创建连接
tcpSocket.connect(addr)
while True:
    senddata = raw_input("请输入(q退出):")
    tcpSocket.send(senddata)
    if senddata == 'q':
        break
tcpSocket.close()
#coding=utf-8
#服务器代码
from socket import *

#创建套接字
tcpSocket = socket(AF_INET,SOCK_STREAM)
addr = ("",7878)
#绑定端口号
tcpSocket.bind(addr)
#设置监听
tcpSocket.listen(5)

while True:
    newSocket,clientAddr = tcpSocket.accept()
    while True:
        data = newSocket.recv(1024)
        if len(data) > 0:
            print data
        else:
            break
    newSocket.close()
tcpSocket.close()

你可能感兴趣的:(python核心编程)