Python Programming/Networks

From Wikibooks, open books for an open world
Jump to: navigation, search

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
Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export