簡體   English   中英

如何重用python Http連接?

[英]How can I reuse a python Http connection?

以下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連接來多次運行帖子,則無法正常工作:

#!/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連接以提高性能,如何解決此問題?

提前致謝!

刪除字符串中的換行符('\\ n')。 工作正常!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM