[Python] Sockets For Beginners - Programming On Unix

Users browsing this thread: 1 Guest(s)
Jayro
Long time nixers
Sockets!
Low Level Network Programming

Table of contents:
* What are sockets?
* How do they work?
* How can I use them?
* Implementation!
- * Connecting
- * Servers

What are sockets?
'In computer networking, an Internet socket or network socket is an endpoint of a bidirectional inter-process communication flow across an Internet Protocol-based computer network, such as the Internet.' -Wikipedia.
In normal person speak, a socket is a daemon on a computer that allows in to communicate with other sockets on other computers over the internet.

How do they work?
A listening socket (like a server) will run on a certain port and listen for incoming connection on that port. The being said there are two types of sockets: One listening for a connection, and one connecting.

How can I use them?
Sockets are used in every application that requires connecting to a server, or another client acting as a server. Some libraries like urllib and httplib use sockets to do what they do. I have personally used sockets for irc bots, chat programs, reading data form a website, etc.

Implementation

Lets get started.

We will start by importing the socket module and creating our socket object.
Code:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
The items in the brackets are properties defined in the socket module. AF_INET means use IPV4, and SOCK_STREAM makes a multi-directional socket.

Now lets connect to something.
Code:
sock.connect(('irc.malvager.net', 6667))
That will create a connection to irc.malvager.net on port 6667 (IRC).

Now we have made a connection, what you do with a connection is completely up to you, but right now we are going to tell the server who we are using IRC protocol.
Code:
sock.send('USER example example example example :\n')

Now lets read what the server says back, we will do this 1024 bytes at a time.
Code:
while True:
    print sock.recv(1024)
This will continue to read form the socket until you exit the program.

Making a simple server!

Now lets make a completely new script, this one is going to listen for connections, and as soon as a client makes a connection it is going to send the word 'Hello' to the client.

Lets start by importing the socket module and creating out socket object.
Code:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Now instead of connecting to a server we have to tell our socket to listen to connections on a certain port.
Code:
sock.bind(('localhost', 1234))
sock.listen(2)
This tells it to listen on port 1234.

Anything in the code after listen(2) will not be executed until something connects. With that said, we have to accept the connection for it to be made.
Code:
s, data = sock.accept()


This creates a new socket for the connection to the client (s) and data is just the connection data which we will not be using.

We can now send our text to the client.
Code:
s.send('Hello')

Tada, we now have a server that listens for connections, and when a connection is established, send the word 'Hello'.

All together the server should look like this:
Code:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 1234))
sock.listen(2)
s, data = sock.accept()
s.send('Hello')

Now here is a challenge for you, using what you have learned, make client to connect to the server so you can see the server say hello.

Hope this was informative,
Jayro
FreeBSD
Long time nixers
I was wondering about this! Very awesome and will try this right now!
I do Byte
jazzycool
Long time nixers
Can you link some books with python socket programming.
venam
Administrators
Layer 4 on *nix uses the bsd sockets.
So it doesn't matter what language you are learning you'll need to learn how to use bsd sockets.
here you go : http://www.cs.vu.nl/~jms/socket-info.html