在python2.7中完好運(yùn)行:
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、芝罘ssl等。為成百上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的芝罘網(wǎng)站制作公司
#!/usr/bin/python # -*- coding: utf-8 -*- # 導(dǎo)入socket庫: import socket # 創(chuàng)建一個(gè)socket: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 建立連接: s.connect(('www.sina.com.cn', 80)) s.send('GET / HTTP/1.1\r\nHost: www.sina.com.cn\r\nConnection: close\r\n\r\n') # 接收數(shù)據(jù): buffer = [] while True: # 每次最多接收1k字節(jié): d = s.recv(1024) if d: buffer.append(d) else: break data = ''.join(buffer) print (data) # 關(guān)閉連接: s.close()
運(yùn)行結(jié)果:
HTTP/1.1 200 OK Server: nginx Date: Mon, 30 Jul 2018 15:27:31 GMT Content-Type: text/html Content-Length: 569784 Connection: close Last-Modified: Mon, 30 Jul 2018 15:24:01 GMT Vary: Accept-Encoding X-Powered-By: shci_v1.03 Expires: Mon, 30 Jul 2018 15:28:06 GMT Cache-Control: max-age=60 Age: 14 Via: http/1.1 gwbn.guangzhou.ha2ts4.26 (ApacheTrafficServer/6.2.1 [cHs f ]), http/1.1 gwbn.shanghai.ha2ts4.19 (ApacheTrafficServer/6.2.1 [cHs f ]) X-Via-Edge: 1532964451960c86fc48b09010e7c77e64765 X-Cache: HIT.19 X-Via-cdn: f=edge,s=gwbn.shanghai.ha2ts4.18.nb.sinaedge.com,c=139.196.111.200;f=Edge,s=gwbn.shanghai.ha2ts4.19,c=124.14.1.18 <!DOCTYPE html> <!-- [ published at 2018-07-30 23:24:00 ] --> <html> <head> : :
在python3中運(yùn)行出錯(cuò):
運(yùn)行結(jié)果:
Traceback (most recent call last): File "/usercode/file.py", line 16, in <module> s.send('GET / HTTP/1.1\r\nHost: www.sina.com.cn\r\nConnection: close\r\n\r\n') TypeError: 'str' does not support the buffer interface
這是因?yàn)閜ython3對字符串做了更改,使得默認(rèn)字符串編碼與python2.7的不同。
所以,使用client_socket.send(data)時(shí),將其替換為client_socket.send(data.encode())。
當(dāng)使用data = client_socket.recv(512)獲取數(shù)據(jù)時(shí),請將其替換為data = client_socket.recv(512).decode()
更改后的程序?yàn)椋?/p>
#!/usr/bin/python # -*- coding: utf-8 -*- # 導(dǎo)入socket庫: import socket # 創(chuàng)建一個(gè)socket: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 建立連接: s.connect(('www.sina.com.cn', 80)) s.send(('GET / HTTP/1.1\r\nHost: www.sina.com.cn\r\nConnection: close\r\n\r\n').encode()) ####添加.encode # 接收數(shù)據(jù): buffer = [] while True: # 每次最多接收1k字節(jié): d = s.recv(1024).decode("utf8","ignore") #######添加.decode("utf8","ignore") if d: buffer.append(d) else: break data = ''.join(buffer) print (data) # 關(guān)閉連接: s.close()
運(yùn)行結(jié)果:
HTTP/1.1 200 OK Server: nginx Date: Mon, 30 Jul 2018 16:00:02 GMT Content-Type: text/html Content-Length: 569807 Connection: close Last-Modified: Mon, 30 Jul 2018 15:57:02 GMT Vary: Accept-Encoding X-Powered-By: shci_v1.03 Expires: Mon, 30 Jul 2018 16:00:35 GMT Cache-Control: max-age=60 Age: 31 Via: http/1.1 gwbn.guangzhou.ha2ts4.26 (ApacheTrafficServer/6.2.1 [cHs f ]), http/1.1 gwbn.shanghai.ha2ts4.19 (ApacheTrafficServer/6.2.1 [cHs f ]) X-Via-Edge: 1532966402856de110e6a09010e7c4a141492 X-Cache: HIT.19 X-Via-CDN: f=edge,s=gwbn.shanghai.ha2ts4.19.nb.sinaedge.com,c=106.14.17.222;f=Edge,s=gwbn.shanghai.ha2ts4.19,c=124.14.1.19 <!DOCTYPE html> <!-- [ published at 2018-07-30 23:57:00 ] --> <html> : :
當(dāng)前標(biāo)題:python網(wǎng)絡(luò)編程:TCP
文章出自:http://jinyejixie.com/article34/pgiese.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、微信小程序、動態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)