Hướng dẫn session.delete python

Có urllib2hỗ trợ phương thức DELETE hoặc PUT không? Nếu có, vui lòng cung cấp bất kỳ ví dụ nào. Tôi cần sử dụng API piston.

  • python
  • urllib2

45 hữu ích 0 bình luận 40k xem chia sẻ

answer

74

bạn có thể làm điều đó với httplib :

import httplib conn = httplib.HTTPConnection('www.foo.com') conn.request('PUT', '/myurl', body) resp = conn.getresponse() content = resp.read()

ngoài ra, hãy kiểm tra câu hỏi này . câu trả lời được chấp nhận cho thấy một cách để thêm các phương thức khác vào urllib2:

import urllib2 opener = urllib2.build_opener(urllib2.HTTPHandler) request = urllib2.Request('//example.org', data='your_put_data') request.add_header('Content-Type', 'your/contenttype') request.get_method = lambda: 'PUT' url = opener.open(request)

74 hữu ích 3 bình luận chia sẻ

answer

14

Chỉnh sửa cho câu trả lời của Raj:

import urllib2 class RequestWithMethod(urllib2.Request): def __init__(self, *args, **kwargs): self._method = kwargs.pop('method', None) urllib2.Request.__init__(self, *args, **kwargs) def get_method(self): return self._method if self._method else super(RequestWithMethod, self).get_method()

14 hữu ích 2 bình luận chia sẻ

answer

8

Đã tìm thấy mã sau từ //gist.github.com/kehr/0c282b14bfa35155deff34d3d27f8307 và nó đã hoạt động đối với tôi (Python 2.7.5):

import urllib2 request = urllib2.Request(uri, data=data) request.get_method = lambda: 'DELETE' response = urllib2.urlopen(request)

8 hữu ích 1 bình luận chia sẻ

answer

7

Bạn có thể phân lớp đối tượng urllib2.Request và ghi đè phương thức khi bạn khởi tạo lớp.

import urllib2 class RequestWithMethod(urllib2.Request): def __init__(self, method, *args, **kwargs): self._method = method urllib2.Request.__init__(*args, **kwargs) def get_method(self): return self._method

Được phép của Benjamin Smedberg

7 hữu ích 0 bình luận chia sẻ

answer

1

Bạn có thể xác định một lớp con của Requestđối tượng và gọi nó như sau:

import urllib2 class RequestWithMethod(urllib2.Request): def __init__(self, *args, **kwargs): self._method = kwargs.pop('method', None) urllib2.Request.__init__(self, *args, **kwargs) def get_method(self): return self._method if self._method else super(RequestWithMethod, self).get_method() def put_request(url, data): opener = urllib2.build_opener(urllib2.HTTPHandler) request = RequestWithMethod(url, method='PUT', data=data) return opener.open(request) def delete_request(url): opener = urllib2.build_opener(urllib2.HTTPHandler) request = RequestWithMethod(url, method='DELETE') return opener.open(request)

(Điều này tương tự như các câu trả lời ở trên, nhưng hiển thị cách sử dụng.)

1 hữu ích 0 bình luận chia sẻ

Đăng nhập để trả lời câu hỏi

Có thể bạn quan tâm

Lệnh DELETE FROM được sử dụng để xóa một bản ghi được chỉ định từ table. Ở đây, chúng ta phải sử dụng câu điều kiện WHERE để chỉ định bản ghi mà bạn muốn xóa, nếu không thì tất cả sẽ bị xóa.

Câu lệnh sql sau được sử dụng để xóa employee có id là 110:

delete from Employee where id = 10001

Ví dụ:

import mysql.connector #tạo đối tượng connection myconn = mysql.connector.connect(host = "localhost", user = "root", passwd = "1234567890", database = "PythonDB") #tạo đối tượng cursor cur = myconn.cursor() try: # cập nhật name cho bảng Employee cur.execute("delete from Employee where id = 10001") myconn.commit() except: myconn.rollback() myconn.close()

Hướng dẫn directory trong python

Khi có quá nhiều file trong một chương trình, chúng cần sắp xếp các file trong từng thư mục (directory) để dễ quản lý. Một thư mục có thể chứa các tập tin ...

Fluent python, 2nd edition pdf

Book descriptionPython’s simplicity lets you become productive quickly, but often this means you aren’t using everything it has to offer. With the updated edition of this hands-on guide, you’ll ...

Hướng dẫn dùng programiz python python

Free and open-source - You can freely use and distribute Python, even for commercial use.Easy to learn - Python has a very simple and elegant syntax. Its much easier to read and write Python ...

Hướng dẫn r dbinom in python

Ngẫu nhiên là một mô-đun có trong thư viện NumPy. Mô-đun này chứa các hàm được sử dụng để tạo số ngẫu nhiên. Mô-đun này chứa một số phương pháp tạo ...

Hướng dẫn dùng describe pandas python

Thư viện pandas python là gì? Nó có thể giúp bạn những gì và làm sao để sử dụng thư viện pandas này trong lập trình python. Hãy cùng tôi đi tìm câu trả lời ...

Hướng dẫn python-attrs

Basics#The simplest possible usage is:>>> from attrs import define, field >>> @define ... class Empty: ... pass >>> Empty() Empty() >>> Empty() == ...

How to make python3 default in kali linux

Open terminal and write “alias python=python3” and hit enter. You are done!Now check your default interpreter version by simply run “python -V” command in the terminal.for more details you ...

How do i make a choice option in python?

OverviewTeaching: 30 min Exercises: 0 min QuestionsHow can my programs do different things based on data values?ObjectivesWrite conditional statements including if, elif, and else branches.Correctly ...

Hướng dẫn z table in python

In statistics, a z-score tells us how many standard deviations away a value is from the mean. We use the following formula to calculate a z-score:z = (X – μ) / σwhere:X is a ...

Hướng dẫn dùng argumented python

Bài viết gốc //manhhomienbienthuy.github.io/2019/09/20/python-args-kwargs.htmlNội dung chínhSử dụng trong định nghĩa hàmSử dụng *argsSử dụng **kwargsSử dụng để ...

Cài python trên visual studio

Với ưu điểm đơn giản, cấu trúc mạch lạc, sử dụng từ vựng tiếng Anh, Python đã trở thành một trong những ngôn ngữ lập trình phát triển nhất hiện nay. ...

Hướng dẫn reverse integer python

Example 1: Reverse a Number using a while loopnum = 1234 reversed_num = 0 while num != 0: digit = num % 10 reversed_num = reversed_num * 10 + digit num //= 10 print(Reversed Number: ...

Hướng dẫn dùng myset python

Trong phần trước mình đã giới thiệu một số keyword cơ bản trong Python. Bài viết lần này mình sẽ giới thiệu chi tiết hơn về 12 hàm thông dụng của từ khóa ...

Hướng dẫn list() python

Mục lục Nhóm phát triển của chúng tôi vừa ra mắt website langlearning.net học tiếng Anh, Nga, Đức, Pháp, Việt, Trung, Hàn, Nhật, ... miễn phí cho tất cả mọi ...

How do you write even in python?

A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the remainder operator % to compute the remainder. If the remainder is not zero, the number is odd.Source ...

Hướng dẫn line plot python

Trước khi bắt đầu với câu hỏi Tại sao Matplotlib là một thư viện phổ biến trong Python? chúng ta đến với vài câu nói đúc kết của người xưa mà còn nguyên ...

Hướng dẫn python-docx placeholder

Im having trouble finding and replacing all occurrences of several placeholders within paragraphs of a Word file. Its for a gamebook, so Im trying to sub random entry numbers for the placeholders ...

Hướng dẫn dùng unpackage python

Dẫn nhậpTrong bài trước, Kteam đã giới thiệu đến bạn KIỂU DỮ LIỆU FUNCTION TRONG PYTHON – Phần 2.Nội dung chínhDẫn nhậpPacking là gì ? Unpacking là gì ...

Display data from database in php

Select Data From a MySQL DatabaseThe SELECT statement is used to select data from one or more tables: SELECT column_name(s) FROM table_name or we can use the * character to select ALL columns from a ...

How do you call a recursive function in python?

In this tutorial, you will learn to create a recursive function (a function that calls itself).What is recursion?Recursion is the process of defining something in terms of itself.A physical world ...

Hướng dẫn python error exercise

Python Error Handling ExercisesLet’s check out some exercises that will help understand Python Errors better.Exercise 15-aType something so that Python gives a NameError.You’ll get a NameError ...

Is prime number function python?

Example to check whether an integer is a prime number or not using for loop and if...else statement. If the number is not prime, its explained in output why it is not a prime number.To understand ...

How do i flatten a list of lists in python?

IntroductionA list is the most flexible data structure in Python. Whereas, a 2D list which is commonly known as a list of lists, is a list object where every item is a list itself - for example: ...

Hướng dẫn nodejs session variables

In this article, we’ll learn about how to use session variables with Node.js.Session Management in Node.js:Session Management is a technique that keeps track of users’ states (data).If the client ...

Is __init__ required in python

No, it isnt necessary.For example.class A(object): def f(): print foo And you can of course use it, in this manner:a = A() a.f() In fact you can even define a class in this ...

Hướng dẫn dùng banana def python

Mục lục Nhóm phát triển của chúng tôi vừa ra mắt website langlearning.net học tiếng Anh, Nga, Đức, Pháp, Việt, Trung, Hàn, Nhật, ... miễn phí cho tất cả mọi ...

How to add space in python output

A quick warning, this a pretty wordy answer.print is tricky sometimes, I had some problems with it when I first started. What you want is a few spaces in between two variables after you print them ...

How do you test if a string is a number python?

A lot of times, while doing some projects or maybe simple programming, we need to curb whether a given Python string is an integer or not. So, in this detailed article, you will get to know about ...

Hướng dẫn dùng sexdictionary python

Bài viết được sự cho phép của tác giả Kien Dang ChungVideo trong bài viếtTrong Python có tới 4 kiểu cấu trúc dữ liệu là List, Tuple, Set và ...

Numbers and strings in python

Python Object Types - Numbers, Strings, and None bogotobogo.com site search: Object Types - Numbers, Strings, and NoneIn Python data takes the form of objects either built-in objects that Python ...

Hướng dẫn python cos sin

Hàm cos() trong Python trả về cos của góc x.Cú phápCú pháp của cos() trong Python:Ghi chú: Hàm này không có thể truy cập trực tiếp, vì thế chúng ta cần import math ...

Hướng dẫn python docstring kwargs

What is the conventional way to express in a docstring the expected types of keyword arguments?Or is this out of principle something I should not be doing at all? Google is suspiciously ...

Hướng dẫn python inherit from string

Tính kế thừa (inheritance) là một trong những tính chất quan trọng của lập trình hướng đối tượng (object-oriented programming). Tính chất này đề cập đến ...

Hướng dẫn dùng chart list python

Python Data Visualization – Trực quan hoá dữ liệu với Python.Nội dung chínhNhập dữ liệuMatplotlib – Python Data VisualizationScatter Plot – Python Data VisualizationLine Chart ...

Hướng dẫn limit value python

You want to round your answer.round(value,significantDigit) is the ordinary solution to do this, however this sometimes does not operate as one would expect from a math perspective when the digit ...

Chủ đề