1. 程式人生 > >python 網絡編程:TCP

python 網絡編程:TCP

style rac true res 12px borde 獲取數據 port wid

在python2.7中完好運行:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# 導入socket庫:
import socket
# 創建一個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')

# 接收數據:
buffer = []
while True:
    # 每次最多接收1k字節:
    d = s.recv(1024)
    if d:
        buffer.append(d)
    else:
        break
data = ''.join(buffer)
print (data)

# 關閉連接:
s.close()

運行結果:

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中運行出錯:

運行結果:

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


這是因為python3對字符串做了更改,使得默認字符串編碼與python2.7的不同。

所以,使用client_socket.send(data)時,將其替換為client_socket.send(data.encode())。
當使用data = client_socket.recv(512)獲取數據時,請將其替換為data = client_socket.recv(512).decode()


更改後的程序為:

#!/usr/bin/python
# -*- coding: utf-8 -*-


# 導入socket庫:
import socket
# 創建一個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

# 接收數據:
buffer = []
while True:
    # 每次最多接收1k字節:
    d = s.recv(1024).decode("utf8","ignore")  #######添加.decode("utf8","ignore")
    if d:
        buffer.append(d)
    else:
        break
data = ''.join(buffer)
print (data)

# 關閉連接:
s.close()

運行結果:

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>
:
:


python 網絡編程:TCP