跳转到内容

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