category: Network | Python Tutorial

Category: Network

Socket Client

How do you open a network connection?

Sockets are the basis of any network communication in your computer.

If you open a website, a socket is created in the background. The same thing applies to chat applications or any other network application.

Related Courses

The TCP/IP model

Packets are delivered to another computer using a layered model: the TCP/IP model.

Every layer of the TCP/IP model has a certain purpose.

tcp/ip model

The application layers responsibility is solely the communication language between two applications.

Sockets work on the application protocol layer. Applications often have their own protocols, which in turn use sockets in the background.

If you want to make your own application protocol (chat system or other purposes) you can use sockets.

Note: This model is used in all network applications.

Python simple socket client

Make a socket application?

We will create a simple socket client, that mimics a webbrowser. The web uses port 80. The steps a webbrowser does to get a webpage are:

  • create socket
  • get server ip address from domain name
  • connect to server using ip address
  • send request to server
  • receive data (webpage)
    In code that will look like:
     # Socket client example in python

    import socket
    import sys

    host = 'www.pythonprogramminglanguage.com'
    port = 80 # web

    # create socket
    print('# Creating socket')
    try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error:
    print('Failed to create socket')
    sys.exit()

    print('# Getting remote IP address')
    try:
    remote_ip = socket.gethostbyname( host )
    except socket.gaierror:
    print('Hostname could not be resolved. Exiting')
    sys.exit()

    # Connect to remote server
    print('# Connecting to server, ' + host + ' (' + remote_ip + ')')
    s.connect((remote_ip , port))

    # Send data to remote server
    print('# Sending data to server')
    request = "GET / HTTP/1.0\r\n\r\n"

    try:
    s.sendall(request)
    except socket.error:
    print 'Send failed'
    sys.exit()

    # Receive data
    print('# Receive data from server')
    reply = s.recv(4096)

    print reply
    Of course all data returned will be shown as the raw html (no images, css etc).

Download network examples

Socket Server

Do you want to make a socket server app?

A server can be created using the module socket.

If you make a socket server, you can define your own application protocol. You can also use it to work with existing apps.

Related Courses

The Algorithm

Sockets work on the application layer, it does not specify any protocol and on this level you’d define an application protocol yourself.

Creation of a socket server needs these steps:

bind socket to port start listening
wait for client receive data

Example socket server

The example below starts a socket server on port 7000. You can use telnet or a socket client to connect to this port.

import socket
import sys

HOST = ''
PORT = 7000

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('# Socket created')

# Create socket on port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print('# Bind failed. ')
sys.exit()

print('# Socket bind complete')

# Start listening on socket
s.listen(10)
print('# Socket now listening')

# Wait for client
conn, addr = s.accept()
print('# Connected to ' + addr[0] + ':' + str(addr[1]))

# Receive data from client
while True:
data = conn.recv(1024)
line = data.decode('UTF-8') # convert to string (Python 3 only)
line = line.replace("\n","") # remove newline character
print( line )

s.close()

Once run, a server will be running on localhost port 7000.

# Socket created
# Socket bind complete
# Socket now listening
# Connected to 127.0.0.1:40499

Once running it will wait for messages. To connect with it, use telnet or modify the socket client from the previous section.

telnet

The module telnetlib implements the telnet protocol. Telnet is a protocol used on the Internet or local area networks, its used for bidirectional interactive text-oriented communication.

Telnet is a low level protocol, in the sense that we can implement application level protocols on top of it like HTTP or FTP.

Related Courses

Telnet example

We use the telnetlib module to do the network logic. All we have to do is connect to the server on the right network port, send the message as bytes (default is unicode in Python3) and wait for reply.

 
import sys
import telnetlib

HOST = "www.python.org"
PORT = "80"

telnetObj=telnetlib.Telnet(HOST,PORT)
message = ("GET /index.html HTTP/1.1\nHost:"+HOST+"\n\n").encode('ascii')
telnetObj.write(message)
output=telnetObj.read_all()
print(output)
telnetObj.close()

In this small example we implement a message of the HTTP protocol, then request the page index.html. we encode the message to ascii using the line:

 
message = ("GET /index.html HTTP/1.1\nHost:"+HOST+"\n\n").encode('ascii')

Telnet essentially is a byte-level connection, we can implement many application level protocols on top of it, HTTP is just one of them.

Download network examples

Download file

We can download data using the urllib2 module.. These examples work with both http, https and for any type of files including text and image.

Data will be saved in the same directory as your program resides. This means if you run your program from C:\apps\, images will be saved there too unless you explicitly specify another directory.

Related Courses

Download data to variable
To download data to a variable, you can use this code:

  
import urllib2
response = urllib2.urlopen('https://wordpress.org/plugins/about/readme.txt')
data = response.read()
print(data)

The first line, ‘import urllib2’, loads the module. The second line opens the connection to the url. The method response.read() downloads data from the url and stores it into the variable data.

Download text file
To download a file you can use this code:

  
import urllib2
response = urllib2.urlopen('https://wordpress.org/plugins/about/readme.txt')
data = response.read()
filename = "readme.txt"
file_ = open(filename, 'w')
file_.write(data)
file_.close()

Download image file
Downloading an image from the web works in the same way:

  
import urllib2
response = urllib2.urlopen('https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Python_logo_and_wordmark.svg/260px-Python_logo_and_wordmark.svg.png')
data = response.read()
filename = "image.png"
file_ = open(filename, 'w')
file_.write(data)
file_.close()

Download network examples

Read Gmail using Python

Read gmail with Python?

The poplib module can be used to download mails from an email server such as gmail.

The protocol used between your computer and the email server is called POP (Post Office Protocol). This protocol can not send mail.

First enable POP support in gmail. .

Related course:

Gmail with Python

We can conect with gmail using:

 
import poplib
import string, random
import StringIO, rfc822
import logging

SERVER = "pop.gmail.com"
USER = "XXXXXX"
PASSWORD = "XXXXXX"

# connect to server
logging.debug('connecting to ' + SERVER)
server = poplib.POP3_SSL(SERVER)
#server = poplib.POP3(SERVER)

# log in
logging.debug('log in')
server.user(USER)
server.pass_(PASSWORD)

Read an email

Read gmail messages using this code:

 
import poplib
import string, random
import StringIO, rfc822
import logging

SERVER = "pop.gmail.com"
USER = "XXXXXXXXXXX"
PASSWORD = "XXXXXXXXXXX"

# connect to server
logging.debug('connecting to ' + SERVER)
server = poplib.POP3_SSL(SERVER)
#server = poplib.POP3(SERVER)

# login
logging.debug('logging in')
server.user(USER)
server.pass_(PASSWORD)

# list items on server
logging.debug('listing emails')
resp, items, octets = server.list()

# download the first message in the list
id, size = string.split(items[0])
resp, text, octets = server.retr(id)

# convert list to Message object
text = string.join(text, "\n")
file = StringIO.StringIO(text)
message = rfc822.Message(file)

# output message
print(message['From']),
print(message['Subject']),
print(message['Date']),
#print(message.fp.read())

We login with the first block. We get all items on the server using server.list(). We go on downloading the first message in the list and finally output it. If you want to output the message data simply uncomment the line message.fp.read()

[sociallocker id=”2511”] Download read-gmail.zip [/sociallocker]


12