Created
December 15, 2011 12:33
-
-
Save dketov/1480946 to your computer and use it in GitHub Desktop.
Протокол HTTP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Запрос к серверу, чтение заголовков ответа | |
""" | |
import sys, urllib2 | |
req = urllib2.Request(sys.argv[1]) | |
try: | |
fd = urllib2.urlopen(req) | |
except urllib2.URLError, e: | |
print "Error retrieving data:", e | |
sys.exit(1) | |
print "Retrieved", fd.geturl() | |
info = fd.info() | |
for key, value in info.items(): | |
print "%s = %s" % (key, value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Запрос к серверу, чтение данных | |
""" | |
import sys, urllib2, socket | |
req = urllib2.Request(sys.argv[1]) | |
try: | |
fd = urllib2.urlopen(req) | |
except urllib2.HTTPError, e: | |
print "Error retrieving data:", e | |
print "Server errror document follows:\n" | |
print e.read() | |
sys.exit(1) | |
except urllib2.URLError, e: | |
print "Error retrieving data:", e | |
sys.exit(2) | |
print "Retrieved", fd.geturl() | |
bytesread = 0 | |
while 1: | |
try: | |
data = fd.read(1024) | |
except socket.error, e: | |
print "Error reading data:", e | |
sys.exit(3) | |
if not len(data): | |
break | |
bytesread += len(data) | |
sys.stdout.write(data) | |
if fd.info().has_key('Content-Length') and \ | |
long(fd.info()['Content-Length']) != long(bytesread): | |
print "Expected a document of size %d, but read %d bytes" % \ | |
(long(fd.info()['Content-Length']), bytesread) | |
sys.exit(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment