-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySocket.py
30 lines (28 loc) · 878 Bytes
/
MySocket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import socket
import re
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send('GET http://www.pythonlearn.com/code/intro-short.txt HTTP/1.0\n\n')
find_items = {'Date':'Date:\s(.+)', 'Etag':"ETag:\s(.+)"}
# write data to a file
fout = open('receive_data.txt', 'w')
output_str = ""
while True:
data = mysock.recv(512)
data_str = str(data)
#data = data.rstrip()
if ( len(data) < 1 ) :
break
for key in find_items:
reg_ex = find_items[key]
print "key:", key
mymatch = re.findall(reg_ex, data)
# returns a list of matched strings
if len(mymatch) != 0:
for item in mymatch:
output_str = output_str + key + ": " + str(item) + "\n"
print data;
fout.write(data)
print "Output String:", output_str
mysock.close()
fout.close()