The following python script works fine:
#!/usr/bin/env python
import httplib, urllib
params = urllib.urlencode({'url':'xxx/xxx/0AAAUw7n6qPQ922.jpg', 'key': 'xxxx'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/html"}
conn = httplib.HTTPConnection("xxx.test.com")
conn.request("POST", "/xx/delete", params, headers);
response = conn.getresponse()
print response.status, response.reason
data = response.read()
print data
conn.close()
But if I want to reuse the open http connection to run post more times, it doesn’t work:
#!/usr/bin/env python
import httplib, urllib
import sys
if len(sys.argv)<2:
print "invalid input"
sys.exit(0)
path = sys.argv[1]
f = open(path)
lines = f.readlines()
f.close()
conn = httplib.HTTPConnection("xxx.test.com")
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/html"}
for line in lines:
if len(line) < 6:
continue
params = urllib.urlencode({'url': line, 'key': 'xxxx'})
conn.request("POST", "/xx/delete", params, headers);
response = conn.getresponse()
print response.status, response.reason
data = response.read()
print data
conn.close()
The return status is: 500 Server Error
I Just want to reuse the http connection to increase performance, how can I fixed this issue?
Thanks in advance!
Remove the newline character (‘\n’) in the string. It works fine!