创建TCP服务器和TCP客户端
生活随笔
收集整理的這篇文章主要介紹了
创建TCP服务器和TCP客户端
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import socket
host='127.0.0.1'
port=8080
web=socket.socket()
web.bind((host,port))
web.listen(5)# 設置最多連接數(shù)
print('服務器等待客戶端連接')
#開啟死循環(huán)
while True:
coon,addr=web.accept()# 建立客戶端連接
data=coon.recv(1024).decode()# 獲取客戶端請求數(shù)據(jù)
print(data)
coon.sendall(b'HTTP/1.1 200 ok\r\n\r\nhello world')
coon.close()
-------------------------------------------------------------------------------------------
import socket
s=socket.socket()
host='127.0.0.1'
port=8080
s.connect((host,port))
send_data=input('請輸入要發(fā)送的數(shù)據(jù):')
s.send(send_data.encode())
recvData=s.recv(1024).decode()
print('接收到的數(shù)據(jù)為:',recvData)
s.close()
總結(jié)
以上是生活随笔為你收集整理的创建TCP服务器和TCP客户端的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring听课笔记(tg)2
- 下一篇: 2016 湖南省省赛 Problem A