#-*-coding:utf-8-*-
importsocket
server=socket.socket()
server.bind(('localhost',6969))#綁定要監(jiān)聽的端口
print("正在監(jiān)聽端口")
server.listen(5) #監(jiān)聽 允許5個(gè)連接
print("我要開始等電話了")
while True:
conn, addr = server.accept() #等電話打進(jìn)來
print(conn) # conn就是客戶端連過來而在服務(wù)端為其生成的一個(gè)連接實(shí)例
print("電話來了")
while True:
data=conn.recv(1024)#通過conn連接實(shí)例接收數(shù)據(jù)
print("recv:",data)
if notdata:
print("client has lost...")
break
conn.send(data.upper())#通過conn連接實(shí)例發(fā)送數(shù)據(jù)
server.close()
#-*-coding:utf-8-*-
importsocket
client=socket.socket()#默認(rèn)famliy=AF_INET(ipv4)地址簇 type=SOCK_STREAM (tcp/ip)聲明socket類型,同時(shí)生成socket連接對(duì)象
client.connect(("localhost",6969))
while True:
msg=raw_input("請(qǐng)輸入:").strip() #不能發(fā)送空數(shù)據(jù)
iflen(msg)==0:continue #如果msg長(zhǎng)度為0,就繼續(xù) 返回到下一次msg=raw_input("請(qǐng)輸入:").strip()
client.send(msg.encode("utf-8"))#3.x只能發(fā)bytes類型數(shù)據(jù),只能接收ASCII數(shù)據(jù),漢字不行,要發(fā)漢字只能編碼成utf-8格式
data=client.recv(1024)#收1024字節(jié)數(shù)據(jù)
print("recv:",data.decode("utf-8"))#bytes類型打印出來要解碼
client.close()
網(wǎng)站標(biāo)題:socket_input_linux環(huán)境
文章起源:http://chinadenli.net/article36/geocpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、、移動(dòng)網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)