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()



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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试