def put_request(self, url, data, header): """ Put请求 :param url: :param data: :param header: :return: """ if not url.startswith('http://'): url = '%s%s' % ('http://', url) print(url) try: if data is None: response = request.put(url=url, headers=header, cookies=self.get_session) else: response = request.put(url=url, params=data, headers=header, cookies=self.get_session) except request.RequestException as e: print('%s%s' % ('RequestException url: ', url)) print(e) return () except Exception as e: print('%s%s' % ('Exception url: ', url)) print(e) return () time_consuming = response.elapsed.microseconds / 1000 time_total = response.elapsed.total_seconds() Common.Consts.STRESS_LIST.append(time_consuming) response_dicts = dict() response_dicts['code'] = response.status_code try: response_dicts['body'] = response.json() except Exception as e: print(e) response_dicts['body'] = '' response_dicts['text'] = response.text response_dicts['time_consuming'] = time_consuming response_dicts['time_total'] = time_total return response_dicts
def test_method_put(self): body = { 'id': 1, 'name': 'Test Eng', 'email': '*****@*****.**', 'body': 'lorem impsum impsum', 'userId': 1 } self.assertTrue( len(request.put(f'{COMMENTS_URL}/{body.get("id")}', data=body)))
def callRequest(method, url, parameters=None, data=None): choice = switchMethod(method) if choice == 2: return str(request.post(url, parameters, data)) elif choice == 3: return str(request.put(url, parameters, data)) elif choice == 4: return str(request.patch(url, parameters, data)) elif choice == 5: return str(request.delete(url, parameters, data)) else: return str(request.get(url, parameters))
def update_data(data_id, summary, description): return request.put(url, json={ summary='summary' description='description' })
def dirty(self, output_id): response = request.put( '{0}/connections/{1}'.format(self._url, output_id), client=output_id, data={'dirty': True}) if response.status != Statuses.OK: raise OutputConnectionError('Failed to dirty {0}'.format(self._url))
class TodoSimple(Resource): def put(self, todo_id): newx = eval(request.form['data']) newx = np.array(newx) res = model.predict(newx) return res.tolist() api.add_resource(TodoSimple, '/') if __name__ == '__main__': app.run(debug=True) # 保存好py文件后启用它 python server.py # API会在如下地址监听 Running on http://127.0.0.1:5000/ # 第四步是调用这个API。另开一个终端,输入 curl http://localhost:5000/todo1 -d "data=[4.9,3.,1.4,0.2]" -X PUT # 成功返回 [ "setosa" ] # 也可以在python中实验。 from request import put put('http://localhost:5000/m1', data={'data': '[ 4.9, 3. , 1.4, 0.2]'}).json()
def test_put_method(self): body = {'id': 1, 'title': 'test', 'body': 'barth', 'userId': 1} self.assertTrue( len(request.put(f'{POSTS_URL}/{body.get("id")}', data=body)))