Mô -đun yêu cầu Show Thí dụThực hiện yêu cầu bài đăng đến một trang web và trả về văn bản phản hồi: Nhập yêu cầu url = 'https://www.w3schools.com/python/demopage.php' myObj = {'someKey': 'somervalue'} x = requests.post (url, json = myobj) in (x.text) Chạy ví dụ » Định nghĩa và cách sử dụngPhương thức Phương thức Cú pháprequests.post (url, data = {key: value}, json = {key: value}, args) Args có nghĩa là 0 hoặc nhiều hơn các đối số được đặt tên trong bảng tham số bên dưới. Thí dụ: Yêu cầu. Giá trị tham số
Không bắt buộc. Một đối tượng JSON để gửi đến URL được chỉ địnhcác tập tin Mô -đun yêu cầu Bài đăng này thảo luận về hai phương thức yêu cầu HTTP (Supertext Transfer Transfer) & NBSP; Nhận và đăng các yêu cầu trong & nbsp; Python và việc triển khai của chúng trong Python. HTTP là gì? HTTP là một tập hợp các giao thức được thiết kế để cho phép giao tiếp giữa máy khách và máy chủ. Nó hoạt động như một giao thức phản hồi yêu cầu giữa máy khách và máy chủ. Trình duyệt web có thể là máy khách và một ứng dụng trên máy tính lưu trữ trang web có thể là máy chủ. Vì vậy, để yêu cầu phản hồi từ máy chủ, chủ yếu có hai phương pháp:
Dưới đây là một sơ đồ đơn giản giải thích khái niệm cơ bản về phương thức GET và POST. Bây giờ, để thực hiện các yêu cầu HTTP trong Python, chúng ta có thể sử dụng một số thư viện HTTP như: Now, to make HTTP requests in python, we can use several HTTP libraries like:
Thanh lịch và đơn giản nhất của các thư viện được liệt kê ở trên là các yêu cầu. Chúng tôi sẽ sử dụng thư viện yêu cầu trong bài viết này. Để tải xuống và cài đặt thư viện yêu cầu, hãy sử dụng lệnh sau: pip install requests Hoặc, tải xuống từ đây và cài đặt thủ công. Đưa ra yêu cầu nhận PARAMS = {'address':location}9 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university0 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university1 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university3 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university4 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university6 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university7 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university8 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university9 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 r = requests.get(url = URL, params = PARAMS)1 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 r = requests.get(url = URL, params = PARAMS)3 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 r = requests.get(url = URL, params = PARAMS)5 r = requests.get(url = URL, params = PARAMS)6 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 r = requests.get(url = URL, params = PARAMS)8 r = requests.get(url = URL, params = PARAMS)9 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 data = r.json()1 data = r.json()2 data = r.json()3 data = r.json()4 data = r.json()3 data = r.json()6 data = r.json()3 data = r.json()8 data = r.json()3 data = {'api_dev_key':API_KEY, 'api_option':'paste', 'api_paste_code':source_code, 'api_paste_format':'python'}05051 data = {'api_dev_key':API_KEY, 'api_option':'paste', 'api_paste_code':source_code, 'api_paste_format':'python'}2 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 data = r.json()1 data = r.json()2 data = r.json()3 data = r.json()4 data = r.json()3 data = r.json()6 data = r.json()3 data = r.json()8__ r = requests.post(url = API_ENDPOINT, data = data)5 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 data = r.json()1 data = r.json()2 data = r.json()3 data = r.json()4 data = r.json()3 pastebin_url = r.text2 data = {'api_dev_key':API_KEY, 'api_option':'paste', 'api_paste_code':source_code, 'api_paste_format':'python'}1 pastebin_url = r.text4 pastebin_url = r.text5 pastebin_url = r.text6 pastebin_url = r.text7 pastebin_url = r.text8 pastebin_url = r.text9 Output: Ví dụ trên tìm thấy vĩ độ, kinh độ và địa chỉ được định dạng của một địa điểm nhất định bằng cách gửi yêu cầu nhận được API Google Maps. API (Giao diện lập trình ứng dụng) cho phép bạn truy cập các tính năng nội bộ của chương trình theo kiểu hạn chế. Và trong hầu hết các trường hợp, dữ liệu được cung cấp ở định dạng JSON (ký hiệu đối tượng JavaScript) (được triển khai dưới dạng các đối tượng từ điển trong Python!). Những điểm quan trọng để suy ra:
Làm một yêu cầu bài đăng PARAMS = {'address':location}9 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university0
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 post() 4
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 r = requests.get(url = URL, params = PARAMS)6 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university6 post() 0post() 1
‘ http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university9 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 PARAMS = {'address':location}07 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 PARAMS = {'address':location}09 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 PARAMS = {'address':location}11 PARAMS = {'address':location}12 http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university2 PARAMS = {'address':location}14 pastebin_url = r.text4 pastebin_url = r.text5 PARAMS = {'address':location}17 pastebin_url = r.text8 PARAMS = {'address':location}19 Ví dụ này giải thích cách dán Source_Code của bạn lên Pastebin.com bằng cách gửi yêu cầu POST đến API Pastebin. Trước hết, bạn sẽ cần tạo khóa API bằng cách đăng ký tại đây và sau đó truy cập khóa API của bạn tại đây.source_code to pastebin.com by sending POST request to the PASTEBIN API. Các tính năng quan trọng của mã này:
Yêu cầu. Phương thức có thể được sử dụng cho nhiều tác vụ khác cũng như điền và gửi các biểu mẫu web, đăng trên dòng thời gian FB của bạn bằng API biểu đồ Facebook, v.v.method could be used for many other tasks as well like filling and submitting the web forms, posting on your FB timeline using the Facebook Graph API, etc. Dưới đây là một số điểm quan trọng cần suy ngẫm về:
Blog này được đóng góp bởi Nikhil Kumar. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng Write.GeekSforGeek.org hoặc gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác.Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên. Yêu cầu bài là gì?Trong điện toán, POST là một phương thức yêu cầu được hỗ trợ bởi HTTP được sử dụng bởi World Wide Web.Theo thiết kế, phương thức yêu cầu POST yêu cầu một máy chủ web chấp nhận dữ liệu được đặt trong phần thân của thông báo yêu cầu, rất có thể là lưu trữ nó.Nó thường được sử dụng khi tải lên một tệp hoặc khi gửi biểu mẫu web đã hoàn thành.a request method supported by HTTP used by the World Wide Web. By design, the POST request method requests that a web server accept the data enclosed in the body of the request message, most likely for storing it. It is often used when uploading a file or when submitting a completed web form.
Bài đăng và nhận được trong Python là gì?Nhận: Để yêu cầu dữ liệu từ máy chủ.post: Để gửi dữ liệu để được xử lý đến máy chủ. POST : to submit data to be processed to the server.
Yêu cầu đặt trong Python là gì?Phương pháp đặt yêu cầu thực thể kèm theo được lưu trữ theo URI được cung cấp.Nếu URI đề cập đến một tài nguyên đã có hiện có, nó sẽ được sửa đổi và nếu URI không trỏ đến một tài nguyên hiện có, thì máy chủ có thể tạo tài nguyên với URI đó.requests that the enclosed entity be stored under the supplied URI. If the URI refers to an already existing resource, it is modified and if the URI does not point to an existing resource, then the server can create the resource with that URI.
Phương pháp bài có nghĩa là gì?Bài đăng phương thức bài được sử dụng để gửi dữ liệu đến máy chủ để tạo/cập nhật tài nguyên.Dữ liệu được gửi đến máy chủ với bài đăng được lưu trữ trong thân yêu cầu của yêu cầu HTTP: post /test/demo_form.php http/1.1.used to send data to a server to create/update a resource. The data sent to the server with POST is stored in the request body of the HTTP request: POST /test/demo_form.php HTTP/1.1. |