Python Socket example: the simplest example

23 May

The python socket component is extremely easy to use, and this simplest example demonstrated its typical use. This is simplest, since both the server and client are the local machine:

On the Server Side:


On the Client Side:





  • First run the, and make sure the server is ready to listen/receive sth
  • Then the client send info to the server;
  • After the server received sth, it terminates

That is it.


Downloads source files: |


Posted by on May 23, 2012 in Programming


Tags: , , ,

7 responses to “Python Socket example: the simplest example

  1. roberweb

    March 4, 2013 at 3:27 am

    I get an error when I run the client:

    Traceback (most recent call last):
       File "", line 7, in <module>
         clientsocket.connect (("localhost", 8089))
       File "/ usr/lib/python2.7/", line 224, in meth
         return getattr (self._sock, name) (* args)
    socket.error: [Errno 111] Connection refused

    How I can fix it?

  2. xinyustudio

    March 5, 2013 at 10:46 am

    I tested that and it worked in windows, but not sure if this is OK in unix. Are you testing in Ubuntu or other OS?

  3. roberweb

    March 8, 2013 at 11:31 pm

    Yes, I tested this in Ubuntu, Linux. Sorry for the latest comment, because is in Spanish…. I’m spanish and I was wrong…

  4. Rick Joens

    March 12, 2014 at 10:31 pm

    So I’m trying this in Windows 7 with Python 3.3.

    The error I’m getting is:

    OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted.

  5. xinyustudio

    March 12, 2014 at 11:32 pm

    Tested with Python 2.72. For Python 3.3, it seems that you only specified the address:port? It is not hard to insert the protocol. It should work with simple modification. Good luck!

  6. DoubleX

    December 18, 2017 at 11:24 pm

    apparently this code does not run on .net using ironpython. Nt sure why, but i would appreciate any suggestions.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: