06 - Regular Expressions and Network Programming
06 - Regular Expressions and Network Programming
LESSON 6
import re
fhand =
open('C:/UCSD/PythonForInformatics
/code/mbox-short.txt')
for line in fhand:
line = line.rstrip()
if re.search('From:', line):
print line
import re
fhand =
open('C:/UCSD/PythonForInformatics
/code/mbox-short.txt')
for line in fhand:
line = line.rstrip()
if re.search('^From:.+@', line):
print line
import re
fhand =
open('C:/UCSD/PythonForInformatics/co
de/mbox.txt')
for line in fhand:
line = line.rstrip()
addr = re.findall(\
'[a-zA-Z0-9]\S*@\S*[a-zA-Z]',line)
if len(addr) > 0:
print addr
'[a-zA-Z0-9]\S*@\S*[a-zA-Z]'
'^X-.*: [0-9.]+'
'^X-.*: [0-9.]+'
import re
fhand =
open('C:/UCSD/PythonForInformatics/co
de/mbox.txt')
for line in fhand:
line = line.rstrip()
addr = re.findall('^X-.*: ([0-9.]+)', line)
if len(addr) > 0:
print addr
'^From .*[0-9.][0-9.]:'
import socket
mysock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send('GET http://www.py4inf.com/code/romeo.txt
HTTP/1.0\n\n')
while True:
data = mysock.recv(512)
if ( len(data) < 1 ) :
break
print data;
mysock.close()
Copyright © 2015 Walter Wesley All Rights Reserved
Network Programming
31
With a little ingenuity, we can generalize our code to allow the user to provide a web page
address as input.
import socket
while True:
data = mysock.recv(512)
if ( len(data) < 1 ) :
break
print data,
mysock.close()
Copyright © 2015 Walter Wesley All Rights Reserved
Network Programming
35
mysock.close()
Copyright © 2015 Walter Wesley All Rights Reserved
Network Programming
37
fhand =
urllib.urlopen('http://www.py4inf.com/code/rome
o.txt')
for line in fhand:
print line.strip()
Copyright © 2015 Walter Wesley All Rights Reserved
Network Programming
40
counts = dict()
fhand =
urllib.urlopen('http://www.py4inf.com/code/r
omeo.txt')
for line in fhand:
words = line.split()
for word in words:
counts[word] = counts.get(word,0) + 1
print counts
Copyright © 2015 Walter Wesley All Rights Reserved
Network Programming
42
import urllib
from BeautifulSoup import *
import urllib
img = urllib.urlopen('http://www.py4inf.com/cover.jpg').read()
fhand = open('cover.jpg', 'w')
fhand.write(img)
fhand.close()
While this approach will work fine as long as your file is small enough to fit into
available memory all at one time, it would be better if we could buffer our
processing of the file so we don’t have such as risk of running out of memory.
import urllib
img = urllib.urlopen('http://www.py4inf.com/cover.jpg')
fhand = open('cover.jpg', 'w')
size = 0
while True:
info = img.read(100000)
if len(info) < 1 : break
size = size + len(info)
fhand.write(info)
This last version of the program code has been optimized for usability and
efficiency.
import os
import urllib