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 サーバー エラー

http 接続を再利用してパフォーマンスを向上させたいだけですが、どうすればこの問題を解決できますか?

前もって感謝します!

4

1 に答える 1

0

文字列内の改行文字('\ n')を削除します。それはうまくいきます!

于 2012-05-16T17:01:28.963 に答える