python3TCPServerClient初学

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33471057/article/details/69901862

server:
from socket import *
from time import ctime

ADDR = ('localhost',8888)
tcpSer = socket(AF_INET,SOCK_STREAM)

tcpSer.bind(ADDR)
tcpSer.listen(1000)

while True:
    print('waiting for connection ...')
    cli, add = tcpSer.accept()
    print('Got connection from',add)
    while True:
        data = cli.recv(1024).decode('utf-8')
        if not data:
            break
        cli.send('[{}] {}'.format(ctime(),data).encode('utf-8'))
    
    cli.close()
    
tcpSer.close()

client:

from socket import *

HOST = '127.0.0.1'
PORT = 8888
ADDR = (HOST,PORT)

cliSocket = socket(AF_INET,SOCK_STREAM)
cliSocket.connect(ADDR)

while True:
    data = input('> ')
    if not data:
        break
    cliSocket.send(data.encode('utf-8'))
    data = cliSocket.recv(1024).decode('utf-8')
    if not data:
        break
    print(data)
    
cliSocket.close()



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页