You are on page 1of 4

PUA SRG

Meeting 3 Python Over The Network

3.14.15 (-Day)

0 Python Refresher
Last time, HTTP Requests were discussed and a few examples were made to display the requests in
Mozilla Firefox and/or Google Chrome. This time, a bit of python programming is due, let's do the
same thing we did in the previous session, but using a little bit of python programming! So here are a
few refreshers/cheat sheets. Next, we will start playing with a few py scripts.
https://perso.limsi.fr/pointal/_media/python:cours:mementopython3-english.pdf
http://overapi.com/python/

1 Simple Program (1): Sorting Lists into Files


listsorting = []
#empty list, to be filled
n=int(input("How many elements do you want:"))
for c in range(n):
listsorting.append(input("Enter a string:"))
listsorting.sort()
#sorting lists:as easy as
for e in listsorting:
#for every element in list
print(e)
f = open("testfile.txt","w")
#how easy is it to write
f.write("testing testing")
#to a file?
f.close()
#3 simple lines
f = open("testfile.txt","r")
#how easy is it to read
string = f.readline()
#from a file?
f.close()
#3 simple lines
print(string)

1/4

PUA SRG

Meeting 3 Python Over The Network

3.14.15 (-Day)

2 Simple Program (2): Sending an HTML Request


import urllib.request
response = urllib.request.urlopen('http://google.com')
html = response.read()
#now what can we do with
print(html)
#this? Webcrawler? Spider?
#parsing and fetching links?
#wget?

2/4

PUA SRG

Meeting 3 Python Over The Network

3.14.15 (-Day)

3 Program (3): Complex HTML Requests with Headers


import urllib.parse
import urllib.request
url = 'http://www.someserver.com/cgi-bin/register.cgi'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
headers = { 'User-Agent' : user_agent }
data = urllib.parse.urlencode(values)
data = data.encode('utf-8')
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req)
the_page = response.read()

https://docs.python.org/3.4/howto/urllib2.html#headers

3/4

PUA SRG

Meeting 3 Python Over The Network

3.14.15 (-Day)

4 Program (4): Simple Server


import socket

# Import socket module

s = socket.socket()
host = socket.gethostname()
port = 12345
s.bind((host, port))

#
#
#
#

Create a socket object


Get local machine name**
Reserve a port for your service.
Bind to the port

s.listen(5)
# Now wait for client connection.
while True:
c, addr = s.accept()
# Establish connection with client.
print('Got connection from', addr)
c.send('Thank you for connecting'.encode())
c.close()
# Close the connection

5 Program (5): Simple Client


import socket

# Import socket module

s = socket.socket()
# Create a socket object
host = socket.gethostname() # Get local machine name**
port = 12345
# Reserve a port for your service.
s.connect((host, port))
print(s.recv(1024).decode())
s.close
# Close the socket when done

http://www.tutorialspoint.com/python/python_networking.htm
**Try using something different than hostname to listen on, try using 'localhost' or '127.0.0.1'

6 Project
Now that we have used python to send and receive requests, and even communicate with simple
servers, it is time to do something useful with that information.
Start suggesting project ideas with brief description/details. We need to do this fast so that you can start
working on your idea, nothing major, simple ideas. By next time, each member should submit at least
an initial version of the code in python implementing his/her idea.

4/4

You might also like