tags:

views:

30

answers:

2

Hi

I need a tool, which can download some part of data from web server, and after that i want connection not be closed. Therfore, i thought about a script in python, which can do: 1) send request 2) read some part of response 3) will freeze - server should think that connection exist, and should not close it

is it possilbe to do it in python ? here is my code:

conn = HTTPConnection("myhost", 10000)
conn.request("GET", "/?file=myfile")
r1 = conn.getresponse()
print r1.status, r1.reason
data = r1.read(2000000)
print len(data)

When im running it, all data is received, and after that server closes connection.

thx in advance for any help

+1  A: 

httplib doesn't support that. Use another library, like httplib2. Here's example.

nosklo
A: 

You can try keep-alive connection. Here is an example:

conn = HTTPConnection("google.com", 80)
headers = dict()
headers['Connection'] = 'Keep-Alive'
headers['Keep-Alive'] = 'timeout=%s' % (15,)
conn.request("GET", "/", "", headers)
r1 = conn.getresponse()
print r1.status, r1.reason
data = r1.read(2000000)
print len(data)
Roman Bodnarchuk