Best way to open a socket in Python

Adam Pierce picture Adam Pierce · Sep 16, 2008 · Viewed 59.9k times · Source

I want to open a TCP client socket in Python. Do I have to go through all the low-level BSD create-socket-handle / connect-socket stuff or is there a simpler one-line way?

Answer

The.Anti.9 picture The.Anti.9 · Sep 16, 2008

Opening sockets in python is pretty simple. You really just need something like this:

import socket
sock = socket.socket()
sock.connect((address, port))

and then you can send() and recv() like any other socket