Hi,
I decided to make an contribution to Evilzone, and so I'm writing this tutorial about network communication in Python.
Requirements:
- Basic knowledge at Python
- Python installed on your computer (Python Homepage)
1.0 Network communicationThe network communication can be divided into several layers.
Illustration1.0.0 shows a picture of a highly simplified OSI-layers model,wich shows the hierarchy of the different protocols:
Illustration1.0.0 ('Leitung' means cable)
Here the Protocols and their tasks:
FTP: file transfer processing
SMTP: Sending mails
POP3: Get mails
IMAP4: Get mails
Telnet: Terminal Emulation
HTTP: Sending textfiles (websites for example)
TCP: Basic connection oriented network protocol
UDP: Basic connectionless network protocol
IP: Connectionless networkprotcol
1.1.0 Socket-Module
The socket module is included in the standartlibrary like all modules I'll teach you now.
But first a little bit about IPs and Ports:
IP stands for Internet Protocol and looks like this:
'127.0.0.1'
Every part of the IP stands for a byte (so it can be a value between 0 and 255) so one IP is 4 bytes big.
There are two IPs:
The local ip is the IP of your computer (I'll show you later how to get it)
The rooter ip is the IP of your rooter (You can check it at
http://www.ip-adress.com)
Then you have exactly 65536 (
99999 ) Ports, but a lot of them are reserved for protocols (for example port 21 for FTP/TCP protocol)
Now you can see it as with
Tin can telephones: The IP is your can and the string is a protocol, and every hole for a string is a port. But here you can talk into each Tin cantelephone connection with one can.
1.1.1 Client/Server RelationThere is everytime a server and a client (both sides are server and client)
The Server is the one who provides the data (Evilzone.org for example), and the Client is the one who wants the data to work with it (You for example).
You can connect to a server with its domain (Google.com) or its IP (74.125.79.106).
Try it: Open a new tab and type '
74.125.79.106' and press enter. You'll get to google.com.
1.1.2 UDPThe UDP protocol was made as a alternative to the TCP protocol to send human language.
UDP is faster than TCP but if you didn't recieve some bits or bytes you'll never get them, except you load everything again.
Let's make a little script to send a message to a server:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip = raw_input("Enter Server IP: ")
msg = raw_input("Message: ")
s.sendto(msg,(ip,50000))
s.close()
First is a socket instance is created. You have to pass it to parameters: the used address-type and the used network portocol. 'AF_INET' stands for Internet/IPv4 and 'SOCK_DGRAM' for the UDP protocol. As the second you say python the ip to connect to and as the third you say python what message should be send.
Then 's.sendto()' sends the message to the given ip.With 's.close()' you end the connection to the server.
But you always need a server too:
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
s.bind(("",500))
while True:
data,address = s.recvfrom(1024)
print "[%s] %s" % (address[0],data)
finally:
s.close()
We create a socket instace first here too. In the 'try/finally' the instance is bound to a address ( "" stands for every ip-address) and a Port (here 50000). In the 'while True' loop it waits to recieve data. The Server-script can only be closed by a keyboardinterrupt or by finishing the task.Then it will end the connection in the 'finally' by 's.close()'
To be continued