跳至內容

Python/Sockets

維基教科書,自由的教學讀本

Sockets

[編輯]

Python可以通過socket通信

連接到伺服器

[編輯]
import socket, sys
sock = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
sock.connect ( ( "google.com", 80 ) )#網址包含https或http需先去除,http以80port連接,https以ssl這模組+443port連接

sock.send('GET / HTTP/1.1\r\n')
sock.send('User-agent: Mozilla/5.0 (wikibooks test)\r\n\r\n')
print (sock.recv(4096))