本文主要是介绍python中TCP与UDP的网络通信(python3.7版测试),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
自己以本机作为服务端和客服端,当然可以自行修改IP地址。注意传输过程中的解码与编码。
TCP服务端:
from socket import * serverPort=12000 serverSocket=socket(AF_INET,SOCK_STREAM) serverSocket.bind(('',serverPort)) serverSocket.listen(10) print("The server is ready to receive")while 1:connectionSocket,addr=serverSocket.accept()sentence=connectionSocket.recv(1024).decode() print(sentence)capitalizedSentence=sentence.upper() #处理客户端来的小写字母,将其转换为大写字母connectionSocket.send(capitalizedSentence.encode())connectionSocket.close()
TCP客户端:
from socket import * serverName="127.0.0.1" #自行修改要连接的服务端IP serverPort=12000 clientSocket=socket(AF_INET,SOCK_STREAM) clientSocket
这篇关于python中TCP与UDP的网络通信(python3.7版测试)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!