Home arrow Python arrow Page 3 - Sockets in Python

Connecting to the Server - Python

Sockets are the lead pipes of computer networks: they let you connect with other devices so that information can flow freely. As you might expect, they're widely used on the Internet. Peyton McCullough explains how to code sockets in Python.

  1. Sockets in Python
  2. The Basics
  3. Connecting to the Server
  4. Sockets...Simplified
  5. Summary
By: Peyton McCullough
Rating: starstarstarstarstar / 113
December 28, 2004

print this article



Let's connect to our server program. Open up the Python command line again and create a socket:

>>> import socket
>>> mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )

Next, open your server program. We will now connect to the server:

>>> mySocket.connect ( ( 'localhost', 2727 ) )

This should look very familiar to the bind method, with the exception of the first argument, which tells our socket where to connect to. We now need to send our server a message:

>>> mySocket.send ( 'William Shakespeare' )

You should now see the message in the server window. Let's accept the server's reply:

>>> mySocket.recv ( 100 )

Finally, let's clean up by closing the socket:

>>> mySocket.close()

That wasn't so hard, was it? Of course not. As I said, sockets are extremely easy to learn and use in your scripts.


I explained datagrams a little bit in a previous section, but I will now show you how to work with datagrams. Let's take our server and rewrite it using datagrams:

import socket
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_DGRAM )
mySocket.bind ( ( '', 2727 ) )
while True:
   data, client = mySocket.recvfrom ( 100 )
   print 'We have received a datagram from', client
   print data
   mySocket.sendto ( 'Green-eyed datagram.', client )

While our datagram server is very similar to our stream server, it also has obvious differences. Notice how we never use the listen method, and note how we use recvfrom and sendto rather than accept, recv and send.

Let's connect to our server through the command line:

>>> import socket
>>> mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
>>> mySocket.sendto ( 'Wherefore art thou?', ( 'localhost', 2727 ) )
>>> data, server = mySocket.recvfrom ( 100 )
>>> print data

As with the server, it is both similar to and different from the other client's code.

>>> More Python Articles          >>> More By Peyton McCullough

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: