0

以下 python 脚本工作正常:

#!/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()

但是如果我想重用打开的 http 连接来运行 post 更多次,它就行不通了:

#!/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()

返回状态为:500 Server Error

我只想重用http连接来提高性能,我该如何解决这个问题?

提前致谢!

4

1 回答 1

0

删除字符串中的换行符 ('\n')。它工作正常!

于 2012-05-16T17:01:28.963 回答