码迷,mamicode.com
首页 > 其他好文 > 详细

socket

时间:2020-03-30 11:25:58      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:utf-8   inpu   accept   ufs   bin   ==   cep   put   str   

# coding:utf-8

from socket import *

HOST = ‘127.0.0.1‘
PORT = 8088
BUFSIZ =1024
ADDR = (HOST,PORT)

tcpCliSock = socket(AF_INET,SOCK_STREAM)
tcpCliSock.connect(ADDR)
while True:
data1 = input(‘>>‘)
#data = str(data)
if not data1:
break
tcpCliSock.send(data1.encode())
data1 = tcpCliSock.recv(BUFSIZ)
if not data1:
break
print(data1.decode(‘utf-8‘))
tcpCliSock.close()
===============================================
# coding:utf-8

from socket import *
from time import ctime

HOST = ‘127.0.0.1‘
PORT = 8088
BUFSIZ = 1024
ADDR = (HOST,PORT)

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

while True:
print(‘waiting for connection...‘)
tcpCliSock, addr = tcpSerSock.accept()
print(‘...connnecting from:‘, addr)

while True:
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
#tcpCliSock.send(‘[%s] %s‘ %(bytes(ctime(),‘utf-8‘),data))
tcpCliSock.send((‘[%s] %s‘ % (ctime(), data)).encode())
tcpCliSock.close()
tcpSerSock.close()

socket

标签:utf-8   inpu   accept   ufs   bin   ==   cep   put   str   

原文地址:https://www.cnblogs.com/yaohu/p/12597208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!