关闭

python socket简单tcp服务器客户端

346人阅读 评论(0) 收藏 举报
分类:
#!/usr/bin/python
import socket
import time
__author__ = 'huangpeng03'

HOST = '172.23.237.8'
PORT = 21571
BUFsiz = 1024
ADDR = (HOST, PORT)


tcpSerSock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
    print 'waiting for connection...'
    tcpCliSock, addr = tcpSerSock.accept()
    print 'connected from:', addr
    while True:
        data = tcpCliSock.recv(BUFsiz)
        if not data:
            break
        tcpCliSock.send('[%s] %s' % (time.ctime(), data))

        #tcpCliSock.close()
tcpSerSock.close()


客户端
<pre name="code" class="python">#!/usr/bin/python
__author__ = 'baidu'

import socket

HOST = '172.23.237.8'
PORT = 21571
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpCliSock.connect(ADDR)
while True:
    data = raw_input('> ')
    if not data:
        break
    tcpCliSock.send(data)
    data = tcpCliSock.recv(BUFSIZ)
    if not data:
        break
    print data

tcpCliSock.close()



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:46450次
    • 积分:1331
    • 等级:
    • 排名:千里之外
    • 原创:86篇
    • 转载:41篇
    • 译文:0篇
    • 评论:3条
    最新评论