Python socket-- 对自己学习有帮助的资源集锦(不断更新中)

一、前言:

学无止境,在学习Python socket的理论及使用方法时,有以下文章及代码为我提供了很大帮助,特此附上链接,感谢大佬们!希望对您也有参考借鉴的意义!

二、学习笔记 / 翻译分析

  1. Python实现Socket通信的简单例子
    代码如下:
# 服务器端
from socket import *
from time import ctime

HOST = ''
PORT = 21567
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()
# 客户端
from socket import *

HOST = '127.0.0.1' # or 'localhost'
PORT = 21567
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()
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页