Hướng dẫn what is the use of requests in python? - việc sử dụng các yêu cầu trong python là gì?
Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: thực hiện các yêu cầu HTTP với Python This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Making HTTP Requests With Python Show
Thư viện 0 là tiêu chuẩn thực tế để thực hiện các yêu cầu HTTP trong Python. Nó trừu tượng hóa sự phức tạp của việc đưa ra các yêu cầu đằng sau một API đơn giản, đẹp đẽ để bạn có thể tập trung vào việc tương tác với các dịch vụ và tiêu thụ dữ liệu trong ứng dụng của mình.Trong suốt bài viết này, bạn sẽ thấy một số tính năng hữu ích nhất mà 0 phải cung cấp cũng như cách tùy chỉnh và tối ưu hóa các tính năng đó cho các tình huống khác nhau mà bạn có thể gặp. Bạn cũng sẽ học cách sử dụng 0 một cách hiệu quả cũng như cách ngăn các yêu cầu đến các dịch vụ bên ngoài làm chậm ứng dụng của bạn.
Trong hướng dẫn này, bạn sẽ học cách:
Mặc dù tôi đã cố gắng bao gồm nhiều thông tin như bạn cần hiểu các tính năng và ví dụ có trong bài viết này, tôi giả sử một kiến thức chung rất cơ bản về HTTP. Điều đó nói rằng, dù sao bạn vẫn có thể theo dõi tốt. Bây giờ, điều đó đã không phù hợp, hãy để Lặn đi vào và xem cách bạn có thể sử dụng 0 trong ứng dụng của mình!Bắt đầu với >>> response.status_code 200 0Hãy bắt đầu bằng cách cài đặt thư viện 0. Để làm như vậy, hãy chạy lệnh sau:Nếu bạn thích sử dụng PIPENV để quản lý các gói Python, bạn có thể chạy như sau:
Sau khi 0 được cài đặt, bạn có thể sử dụng nó trong ứng dụng của mình. Nhập 0 có vẻ như thế này:Bây giờ, tất cả các bạn đã thiết lập, đó là thời gian để bắt đầu hành trình của bạn thông qua 0. Mục tiêu đầu tiên của bạn sẽ là học cách thực hiện yêu cầu 9.Yêu cầu nhậnCác phương thức HTTP như 9 và 1, xác định hành động nào mà bạn đang cố gắng thực hiện khi thực hiện yêu cầu HTTP. Ngoài 9 và 1, có một số phương pháp phổ biến khác mà bạn sẽ sử dụng sau này trong hướng dẫn này.Một trong những phương pháp HTTP phổ biến nhất là 9. Phương thức 9 chỉ ra rằng bạn đang cố gắng lấy hoặc truy xuất dữ liệu từ một tài nguyên được chỉ định. Để thực hiện yêu cầu 9, gọi 7.Để kiểm tra điều này, bạn có thể thực hiện yêu cầu 9 cho API REST root Github bằng cách gọi 9 với URL sau:>>>
Xin chúc mừng! Bạn đã thực hiện yêu cầu đầu tiên của bạn. Hãy để lặn sâu hơn một chút về phản ứng của yêu cầu đó. Phản hồiA 0 là một đối tượng mạnh mẽ để kiểm tra kết quả của yêu cầu. Hãy để Lừa thực hiện một lần nữa, nhưng thời gian này lưu trữ giá trị trả về trong một biến để bạn có thể xem xét kỹ hơn các thuộc tính và hành vi của nó:>>>
Xin chúc mừng! Bạn đã thực hiện yêu cầu đầu tiên của bạn. Hãy để lặn sâu hơn một chút về phản ứng của yêu cầu đó. Phản hồiA 0 là một đối tượng mạnh mẽ để kiểm tra kết quả của yêu cầu. Hãy để Lừa thực hiện một lần nữa, nhưng thời gian này lưu trữ giá trị trả về trong một biến để bạn có thể xem xét kỹ hơn các thuộc tính và hành vi của nó:Trong ví dụ này, bạn đã nắm bắt được giá trị trả về của 9, đây là một ví dụ là 0 và lưu trữ nó trong một biến gọi là 3. Bây giờ bạn có thể sử dụng 3 để xem nhiều thông tin về kết quả yêu cầu 9 của bạn.Mã trạng thái >>>
Xin chúc mừng! Bạn đã thực hiện yêu cầu đầu tiên của bạn. Hãy để lặn sâu hơn một chút về phản ứng của yêu cầu đó. Phản hồi
A 0 là một đối tượng mạnh mẽ để kiểm tra kết quả của yêu cầu. Hãy để Lừa thực hiện một lần nữa, nhưng thời gian này lưu trữ giá trị trả về trong một biến để bạn có thể xem xét kỹ hơn các thuộc tính và hành vi của nó:Trong ví dụ này, bạn đã nắm bắt được giá trị trả về của 9, đây là một ví dụ là 0 và lưu trữ nó trong một biến gọi là 3. Bây giờ bạn có thể sử dụng 3 để xem nhiều thông tin về kết quả yêu cầu 9 của bạn.Mã trạng thái
Truyền thông tin đầu tiên mà bạn có thể thu thập từ 0 là mã trạng thái. Mã trạng thái thông báo cho bạn về trạng thái của yêu cầu.Ví dụ, 8 cho bạn biết rằng phản hồi đã thành công, nhưng không có nội dung nào để trả lại trong cơ thể tin nhắn.Vì vậy, hãy chắc chắn rằng bạn chỉ sử dụng tốc ký thuận tiện này nếu bạn muốn biết liệu yêu cầu có thường thành công hay không và sau đó, nếu cần, xử lý phản hồi một cách thích hợp dựa trên mã trạng thái. Hãy nói rằng bạn không muốn kiểm tra mã trạng thái phản hồi trong một câu lệnh 2. Thay vào đó, bạn muốn nêu ra một ngoại lệ nếu yêu cầu không thành công. Bạn có thể làm điều này bằng cách sử dụng 0:
Nếu bạn gọi 0, một 2 sẽ được nêu ra cho các mã trạng thái nhất định. Nếu mã trạng thái cho biết một yêu cầu thành công, chương trình sẽ tiến hành mà không có ngoại lệ đó được nêu ra.Bây giờ, bạn đã biết rất nhiều về cách xử lý mã trạng thái của phản hồi bạn đã lấy lại từ máy chủ. Tuy nhiên, khi bạn thực hiện yêu cầu 9, bạn hiếm khi chỉ quan tâm đến mã trạng thái của phản hồi. Thông thường, bạn muốn xem thêm. Tiếp theo, bạn sẽ thấy cách xem dữ liệu thực tế mà máy chủ gửi lại trong phần thân của phản hồi.Nội dungPhản hồi của yêu cầu 9 thường có một số thông tin có giá trị, được gọi là tải trọng, trong thân thông báo. Sử dụng các thuộc tính và phương thức của 0, bạn có thể xem tải trọng ở nhiều định dạng khác nhau.Để xem nội dung phản hồi trong 6, bạn sử dụng 7:>>>
Mặc dù 7 cho phép bạn truy cập vào các byte thô của tải trọng phản hồi, bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng một mã hóa ký tự như UTF-8. 3 sẽ làm điều đó cho bạn khi bạn truy cập 0:>>>
Mặc dù 7 cho phép bạn truy cập vào các byte thô của tải trọng phản hồi, bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng một mã hóa ký tự như UTF-8. 3 sẽ làm điều đó cho bạn khi bạn truy cập 0:>>>
Mặc dù 7 cho phép bạn truy cập vào các byte thô của tải trọng phản hồi, bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng một mã hóa ký tự như UTF-8. 3 sẽ làm điều đó cho bạn khi bạn truy cập 0:>>> 0Mặc dù 7 cho phép bạn truy cập vào các byte thô của tải trọng phản hồi, bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng một mã hóa ký tự như UTF-8. 3 sẽ làm điều đó cho bạn khi bạn truy cập 0:Vì việc giải mã 6 thành 2 yêu cầu sơ đồ mã hóa, 0 sẽ cố gắng đoán mã hóa dựa trên các tiêu đề phản hồi nếu bạn không chỉ định. Bạn có thể cung cấp mã hóa rõ ràng bằng cách cài đặt 4 trước khi truy cập 0:Nếu bạn nhìn vào phản hồi, bạn sẽ thấy rằng đó thực sự là nội dung JSON tuần tự hóa. Để có được một từ điển, bạn có thể lấy >>> response.encoding = 'utf-8' # Optional: requests infers this internally >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 2 mà bạn đã lấy từ >>> response.encoding = 'utf-8' # Optional: requests infers this internally >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 0 và giải phóng nó bằng cách sử dụng >>> response.encoding = 'utf-8' # Optional: requests infers this internally >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 8. Tuy nhiên, một cách đơn giản hơn để thực hiện nhiệm vụ này là sử dụng >>> response.encoding = 'utf-8' # Optional: requests infers this internally >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 9: 00 của giá trị trả về của 9 là từ điển, do đó bạn có thể truy cập các giá trị trong đối tượng theo khóa. 1Bạn có thể làm rất nhiều với mã trạng thái và thân thông báo. Nhưng, nếu bạn cần thêm thông tin, như siêu dữ liệu về chính phản hồi, bạn sẽ cần phải xem xét các tiêu đề phản hồi. Tham số chuỗi truy vấn >>> 2Mặc dù 7 cho phép bạn truy cập vào các byte thô của tải trọng phản hồi, bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng một mã hóa ký tự như UTF-8. 3 sẽ làm điều đó cho bạn khi bạn truy cập 0:>>> 3Mặc dù 7 cho phép bạn truy cập vào các byte thô của tải trọng phản hồi, bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng một mã hóa ký tự như UTF-8. 3 sẽ làm điều đó cho bạn khi bạn truy cập 0:Vì việc giải mã >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 6 thành >>> response.encoding = 'utf-8' # Optional: requests infers this internally >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 2 yêu cầu sơ đồ mã hóa, >>> response.status_code 200 0 sẽ cố gắng đoán mã hóa dựa trên các tiêu đề phản hồi nếu bạn không chỉ định. Bạn có thể cung cấp mã hóa rõ ràng bằng cách cài đặt >>> response.encoding = 'utf-8' # Optional: requests infers this internally >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 4 trước khi truy cập >>> response.encoding = 'utf-8' # Optional: requests infers this internally >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 0:Nếu bạn nhìn vào phản hồi, bạn sẽ thấy rằng đó thực sự là nội dung JSON tuần tự hóa. Để có được một từ điển, bạn có thể lấy 2 mà bạn đã lấy từ 0 và giải phóng nó bằng cách sử dụng 8. Tuy nhiên, một cách đơn giản hơn để thực hiện nhiệm vụ này là sử dụng 9:>>> 4Mặc dù 7 cho phép bạn truy cập vào các byte thô của tải trọng phản hồi, bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng một mã hóa ký tự như UTF-8. 3 sẽ làm điều đó cho bạn khi bạn truy cập 0:>>> 5Mặc dù 7 cho phép bạn truy cập vào các byte thô của tải trọng phản hồi, bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng một mã hóa ký tự như UTF-8. 3 sẽ làm điều đó cho bạn khi bạn truy cập 0:Vì việc giải mã >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 6 thành >>> response.encoding = 'utf-8' # Optional: requests infers this internally >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 2 yêu cầu sơ đồ mã hóa, >>> response.status_code 200 0 sẽ cố gắng đoán mã hóa dựa trên các tiêu đề phản hồi nếu bạn không chỉ định. Bạn có thể cung cấp mã hóa rõ ràng bằng cách cài đặt >>> response.encoding = 'utf-8' # Optional: requests infers this internally >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 4 trước khi truy cập >>> response.encoding = 'utf-8' # Optional: requests infers this internally >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 0:Nếu bạn nhìn vào phản hồi, bạn sẽ thấy rằng đó thực sự là nội dung JSON tuần tự hóa. Để có được một từ điển, bạn có thể lấy 2 mà bạn đã lấy từ 0 và giải phóng nó bằng cách sử dụng 8. Tuy nhiên, một cách đơn giản hơn để thực hiện nhiệm vụ này là sử dụng 9: 00 của giá trị trả về của 9 là từ điển, do đó bạn có thể truy cập các giá trị trong đối tượng theo khóa.Bạn có thể làm rất nhiều với mã trạng thái và thân thông báo. Nhưng, nếu bạn cần thêm thông tin, như siêu dữ liệu về chính phản hồi, bạn sẽ cần phải xem xét các tiêu đề phản hồi. >>> 6Bạn cũng có thể gửi cùng một dữ liệu như một danh sách các bộ dữ liệu: >>> 7Tuy nhiên, nếu bạn cần gửi dữ liệu JSON, bạn có thể sử dụng tham số 34. Khi bạn truyền dữ liệu JSON qua 34, 0 sẽ tuần tự hóa dữ liệu của bạn và thêm tiêu đề 37 chính xác cho bạn.httpbin.org là một tài nguyên tuyệt vời được tạo ra bởi tác giả của 0, Kenneth Reitz. Nó có một dịch vụ chấp nhận các yêu cầu kiểm tra và trả lời dữ liệu về các yêu cầu. Chẳng hạn, bạn có thể sử dụng nó để kiểm tra yêu cầu 1 cơ bản:>>> 8Bạn có thể thấy từ phản hồi mà máy chủ nhận được dữ liệu yêu cầu và tiêu đề của bạn khi bạn gửi chúng. 0 cũng cung cấp thông tin này cho bạn dưới dạng 41.Kiểm tra yêu cầu của bạnKhi bạn đưa ra yêu cầu, thư viện 0 sẽ chuẩn bị yêu cầu trước khi thực sự gửi nó đến máy chủ đích. Chuẩn bị yêu cầu bao gồm những thứ như xác thực tiêu đề và tuần tự hóa nội dung JSON.Bạn có thể xem 41 bằng cách truy cập 44:>>> 9Kiểm tra 41 cho phép bạn truy cập vào tất cả các loại thông tin về yêu cầu được thực hiện như tải trọng, URL, tiêu đề, xác thực, v.v.Cho đến nay, bạn đã đưa ra rất nhiều loại yêu cầu khác nhau, nhưng tất cả họ đều có một điểm chung: chúng là những yêu cầu không xác thực đối với API công khai. Nhiều dịch vụ bạn có thể đi qua sẽ muốn bạn xác thực theo một cách nào đó. Xác thựcXác thực giúp một dịch vụ hiểu bạn là ai. Thông thường, bạn cung cấp thông tin đăng nhập của mình cho một máy chủ bằng cách truyền dữ liệu thông qua tiêu đề 46 hoặc tiêu đề tùy chỉnh được xác định bởi dịch vụ. Tất cả các chức năng yêu cầu mà bạn đã thấy đến điểm này cung cấp một tham số gọi là 47, cho phép bạn vượt qua thông tin đăng nhập của mình.Một ví dụ về API yêu cầu xác thực là API người dùng được xác thực của GitHub. Điểm cuối này cung cấp thông tin về hồ sơ người dùng được xác thực. Để thực hiện yêu cầu cho API người dùng được xác thực, bạn có thể chuyển tên người dùng và mật khẩu GitHub của mình một cách >>> 0Yêu cầu đã thành công nếu thông tin đăng nhập bạn đã chuyển trong tuple cho 47 là hợp lệ. Nếu bạn cố gắng thực hiện yêu cầu này không có thông tin đăng nhập, bạn sẽ thấy mã trạng thái là 50:>>> 1Khi bạn chuyển tên người dùng và mật khẩu của mình một cách khác với tham số 47, 0 đang áp dụng thông tin đăng nhập bằng sơ đồ xác thực truy cập cơ bản của HTTP, dưới mui xe.Do đó, bạn có thể thực hiện cùng một yêu cầu bằng cách truyền thông tin xác thực cơ bản rõ ràng bằng cách sử dụng 53:>>> 2Mặc dù bạn không cần phải rõ ràng để xác thực cơ bản, bạn có thể muốn xác thực bằng phương pháp khác. 0 cung cấp các phương pháp xác thực khác ra khỏi hộp, chẳng hạn như 55 và 56.Bạn thậm chí có thể cung cấp cơ chế xác thực của riêng bạn. Để làm như vậy, trước tiên bạn phải tạo một lớp con của 57. Sau đó, bạn thực hiện 58: 3Tại đây, cơ chế 59 tùy chỉnh của bạn nhận được mã thông báo, sau đó bao gồm mã thông báo đó trong tiêu đề 60 của yêu cầu của bạn.Các cơ chế xác thực xấu có thể dẫn đến các lỗ hổng bảo mật, vì vậy trừ khi một dịch vụ yêu cầu một cơ chế xác thực tùy chỉnh vì một số lý do, bạn sẽ luôn muốn sử dụng sơ đồ AUTH đã thử và đúng như Basic hoặc OAuth. Trong khi bạn đang nghĩ về bảo mật, hãy để xem xét xử lý các chứng chỉ SSL bằng cách sử dụng 0.Xác minh chứng chỉ SSLBất cứ khi nào dữ liệu bạn đang cố gắng gửi hoặc nhận là nhạy cảm, bảo mật là quan trọng. Cách mà bạn giao tiếp với các trang web an toàn qua HTTP là bằng cách thiết lập kết nối được mã hóa bằng SSL, điều đó có nghĩa là việc xác minh chứng chỉ SSL của máy chủ đích là rất quan trọng. Tin tốt là 0 thực hiện điều này cho bạn theo mặc định. Tuy nhiên, có một số trường hợp bạn có thể muốn thay đổi hành vi này.Nếu bạn muốn tắt xác minh chứng chỉ SSL, bạn sẽ chuyển 1 sang tham số 64 của hàm yêu cầu:>>> 4 0 thậm chí còn cảnh báo bạn khi bạn đưa ra yêu cầu không an toàn để giúp bạn giữ dữ liệu của mình an toàn!Màn biểu diễnKhi sử dụng 0, đặc biệt là trong môi trường ứng dụng sản xuất, điều quan trọng là phải xem xét ý nghĩa hiệu suất. Các tính năng như điều khiển thời gian chờ, phiên và giới hạn thử lại có thể giúp bạn giữ cho ứng dụng của bạn hoạt động trơn tru.Thời gian chờKhi bạn thực hiện yêu cầu nội tuyến cho một dịch vụ bên ngoài, hệ thống của bạn sẽ cần chờ phản hồi trước khi tiếp tục. Nếu ứng dụng của bạn chờ đợi quá lâu cho phản hồi đó, các yêu cầu cho dịch vụ của bạn có thể sao lưu, trải nghiệm người dùng của bạn có thể bị ảnh hưởng hoặc công việc nền của bạn có thể bị treo. Theo mặc định, 0 sẽ đợi vô thời hạn về phản hồi, vì vậy bạn hầu như nên luôn chỉ định thời gian chờ để ngăn chặn những điều này xảy ra. Để đặt thời gian chờ yêu cầu, hãy sử dụng tham số 68. 68 có thể là một số nguyên hoặc float đại diện cho số giây để chờ phản hồi trước khi hết thời gian:>>> 5Trong yêu cầu đầu tiên, yêu cầu sẽ hết thời gian chờ sau 1 giây. Trong yêu cầu thứ hai, yêu cầu sẽ hết thời gian chờ sau 3,05 giây. Bạn cũng có thể chuyển một tuple sang 68 với phần tử đầu tiên là thời gian chờ kết nối (thời gian cho phép máy khách thiết lập kết nối với máy chủ) và thứ hai là thời gian chờ đọc (thời gian nó sẽ chờ phản hồi một lần Khách hàng của bạn đã thiết lập một kết nối):>>> 6Trong yêu cầu đầu tiên, yêu cầu sẽ hết thời gian chờ sau 1 giây. Trong yêu cầu thứ hai, yêu cầu sẽ hết thời gian chờ sau 3,05 giây. 7Bạn cũng có thể chuyển một tuple sang 68 với phần tử đầu tiên là thời gian chờ kết nối (thời gian cho phép máy khách thiết lập kết nối với máy chủ) và thứ hai là thời gian chờ đọc (thời gian nó sẽ chờ phản hồi một lần Khách hàng của bạn đã thiết lập một kết nối):Nếu yêu cầu thiết lập kết nối trong vòng 2 giây và nhận dữ liệu trong vòng 5 giây sau khi kết nối được thiết lập, thì phản hồi sẽ được trả về như trước đây. Nếu yêu cầu hết thời gian, thì chức năng sẽ tăng ngoại lệ >>> requests.get('https://api.github.com')
|