Python Programming/Networks
From Wikibooks, open books for an open world
|
|
A reader has identified this page or section as an undeveloped draft or outline. You can help to develop the work, or you can ask for assistance in the project room. |
Python can also communicate via sockets. Commonly, the most useful socket type are Internet sockets.
[edit] Connecting to a server
This simple Python program will fetch a 4096 byte HTTP response from Google:
import socket import sys irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) irc.connect ( ( "google.com", 80 ) ) irc.send('GET / HTTP/1.1\r\n') irc.send('User-agent: Mozilla/5.0 (wikibooks test)\r\n\r\n') f = irc.recv(4096) print f