【发布时间】:2018-02-27 02:27:03
【问题描述】:
我想使用 api 自动化测试工具。 首先,我登录网站并获取一个 cookie。
我的代码是python3
import urllib
import urllib3
from bs4 import BeautifulSoup
url ='http://ip:port/api/login'
login_req = urllib.parse.urlencode(login_form)
http = urllib3.PoolManager()
r= http.request('POST',url,fields={'userName':'id','password':'passoword'})
soup = BeautifulSoup(r.data.decode('utf-8'),'lxml')
cookie = r.getheaders().get('Set-Cookie')
str1 = r.getheaders().get('Set-Cookie')
str2 = 'JSESSIONID' +str1.split('JSESSIONID')[1]
str2 = str2[0:-2]
print(str2)
-- JSESSIONID=df0010cf-1273-4add-9158-70d817a182f7; Path=/; HttpOnly
然后,我在另一个网站 api 上添加了 cookie。 但它不起作用!
url2 = 'http://ip:port/api/notebook/job/paragraph'
r2 = http.request('POST',url2)
r2.headers['Set-Cookie']=str2
r2.headers['Cookie']=str2
http.request('POST',url2, headers=r2.headers)
为什么不工作?它显示了另一个 cookie 如果您知道这种情况,请向我解释.. 错误内容就是这样。
HTTP ERROR 500
Problem accessing /api/login;JSESSIONID=b8f6d236-494b-4646-8723-ccd0d7ef832f.
Reason: Server Error
Caused by:</h3><pre>javax.servlet.ServletException: Filtered request failed.
ProtocolError: ('Connection aborted.', BadStatusLine('<html>\n',))
非常感谢!
【问题讨论】:
-
通常以
5开头的错误代码是由于网站问题而引发的。如果您收到HTTP ERROR 500,我认为您的代码没有任何问题。参考:en.wikipedia.org/wiki/List_of_HTTP_status_codes -
@KeyurPotdar 谢谢..我找到了另一个解决方案!
标签: python python-3.x curl python-requests