<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Python `httplib2` – HTTP GET 和 POST 示例 > 原文: [https://howtodoinjava.com/python/httplib2-http-get-post-requests/](https://howtodoinjava.com/python/httplib2-http-get-post-requests/) 學習使用 Python [`httplib2`](https://github.com/httplib2/httplib2)模塊。 超文本傳輸??協議(HTTP)是用于分布式協作超媒體信息系統的應用協議。 HTTP 是萬維網數據通信的基礎。 Python `httplib2`模塊提供了用于通過 HTTP 訪問 Web 資源的方法。 它支持許多功能,例如 HTTP 和 HTTPS,認證,緩存,重定向和壓縮。 ```py $ service nginx status * nginx is running ``` 我們在本地主機上運行 nginx Web 服務器。 我們的一些示例將連接到本地運行的 nginx 服務器上的 PHP 腳本。 ```py Table of Contents Check httplib2 Library Version Use httplib2 to Read Web Page Send HTTP HEAD Request Send HTTP GET Request Send HTTP POST Request Send User Agent Information Add Username/Password to Request ``` ## 檢查`httplib2`庫版本 第一個程序打印庫的版本,其版權和文檔字符串。 ```py #!/usr/bin/python3 import httplib2 print(httplib2.__version__) print(httplib2.__copyright__) print(httplib2.__doc__) ``` `httplib2.__version__`給出`httplib2`庫的版本,`httplib2.__copyright__`給出其版權,`httplib2.__doc__`給出其文檔字符串。 ```py $ ./version.py 0.8 Copyright 2006, Joe Gregorio httplib2 A caching http interface that supports ETags and gzip to conserve bandwidth. Requires Python3.0 or later Changelog: 2009-05-28, Pilgrim: ported to Python3 2007-08-18, Rick: Modified so it's able to use a socks proxy if needed. ``` 這是示例的示例輸出。 ## 使用`httplib2`讀取網頁 在下面的示例中,我們顯示了如何從名為 [www.something.com](http://www.something.com) 的網站上獲取 HTML 內容。 ```py #!/usr/bin/python3 import httplib2 http = httplib2.Http() content = http.request("http://www.something.com")[1] print(content.decode()) ``` 使用`httplib2.HTTP()`創建一個 HTTP 客戶端。 使用`request()`方法創建一個新的 HTTP 請求; 默認情況下,它是一個 GET 請求。 返回值是響應和內容的元組。 ```py $ ./get_content.py <html><head><title>Something.</title></head> <body>Something.</body> </html> ``` 這是示例的輸出。 #### 剝離 HTML 標簽 以下程序獲取一個小型網頁,并剝離其 HTML 標簽。 ```py #!/usr/bin/python3 import httplib2 import re http = httplib2.Http() content = http.request("http://www.something.com")[1] stripped = re.sub('<[^<]+?>', '', content.decode()) print(stripped) ``` 一個簡單的正則表達式用于剝離 HTML 標記。 請注意,我們正在剝離數據,我們沒有對其進行清理。 (這是兩件事。) ```py $ ./strip_tags.py Something. Something. ``` 該腳本會打印網頁的標題和內容。 #### 檢查響應狀態 響應對象包含`status`屬性,該屬性提供響應的狀態代碼。 ```py #!/usr/bin/python3 import httplib2 http = httplib2.Http() resp = http.request("http://www.something.com")[0] print(resp.status) resp = http.request("http://www.something.com/news/")[0] print(resp.status) ``` 我們使用`request()`方法執行兩個 HTTP 請求,并檢查返回的狀態。 ```py $ ./get_status.py 200 404 ``` 200 是成功 HTTP 請求的標準響應,而 404 則表明找不到所請求的資源。 ## 發送 HTTP HEAD 請求 HTTP HEAD 方法檢索文檔標題。 標頭由字段組成,包括日期,服務器,內容類型或上次修改時間。 ```py #!/usr/bin/python3 import httplib2 http = httplib2.Http() resp = http.request("http://www.something.com", "HEAD")[0] print("Server: " + resp['server']) print("Last modified: " + resp['last-modified']) print("Content type: " + resp['content-type']) print("Content length: " + resp['content-length']) ``` 該示例打印服務器,`www.something.com`網頁的上次修改時間,內容類型和內容長度。 ```py $ ./do_head.py Server: Apache/2.4.12 (FreeBSD) OpenSSL/1.0.1l-freebsd mod_fastcgi/mod_fastcgi-SNAP-0910052141 Last modified: Mon, 25 Oct 1999 15:36:02 GMT Content type: text/html Content length: 72 ``` 這是程序的輸出。 從輸出中,我們可以看到該網頁是由 FreeBSD 托管的 Apache Web 服務器交付的。 該文檔的最后修改時間是 1999 年。網頁是 HTML 文檔,其長度為 72 個字節。 ## 發送 HTTP GET 請求 HTTP GET 方法請求指定資源的表示形式。 對于此示例,我們還將使用`greet.php`腳本: ```py <?php echo "Hello " . htmlspecialchars($_GET['name']); ?> ``` 在`/usr/share/nginx/html/`目錄中,有此`greet.php`文件。 該腳本返回`name`變量的值,該值是從客戶端檢索到的。 `htmlspecialchars()`函數將特殊字符轉換為 HTML 實體; 例如`&`至`&amp`。 ```py #!/usr/bin/python3 import httplib2 http = httplib2.Http() content = http.request("http://localhost/greet.php?name=Peter", method="GET")[1] print(content.decode()) ``` 該腳本將帶有值的變量發送到服務器上的 PHP 腳本。 該變量直接在 URL 中指定。 ```py $ ./mget.py Hello Peter ``` 這是示例的輸出。 ```py $ tail -1 /var/log/nginx/access.log 127.0.0.1 - - [21/Aug/2016:17:32:31 +0200] "GET /greet.php?name=Peter HTTP/1.1" 200 42 "-" "Python-httplib2/0.8 (gzip)" ``` 我們檢查了 nginx 訪問日志。 ## 發送 HTTP POST 請求 POST 請求方法請求 Web 服務器接受并存儲請求消息正文中包含的數據。 上載文件或提交完整的 Web 表單時經常使用它。 ```py <?php echo "Hello " . htmlspecialchars($_POST['name']); ?> ``` 在本地 Web 服務器上,我們有此`target.php`文件。 它只是將過帳的值打印回客戶。 ```py #!/usr/bin/python3 import httplib2 import urllib http = httplib2.Http() body = {'name': 'Peter'} content = http.request("http://localhost/target.php", method="POST", headers={'Content-type': 'application/x-www-form-urlencoded'}, body=urllib.parse.urlencode(body) )[1] print(content.decode()) ``` 腳本使用具有`Peter`值的`name`鍵發送請求。 數據使用`urllib.parse.urlencode()`方法進行編碼,并在請求的正文中發送。 ```py $ ./mpost.py Hello Peter ``` 這是`mpost.py`腳本的輸出。 ```py $ tail -1 /var/log/nginx/access.log 127.0.0.1 - - [23/Aug/2016:12:21:07 +0200] "POST /target.php HTTP/1.1" 200 37 "-" "Python-httplib2/0.8 (gzip)" ``` 使用 POST 方法時,不會在請求 URL 中發送該值。 ## 發送用戶代理信息 在本節中,我們指定用戶代理的名稱。 ```py <?php echo $_SERVER['HTTP_USER_AGENT']; ?> ``` 在 nginx 文檔根目錄中,我們有`agent.php`文件。 它返回用戶代理的名稱。 ```py #!/usr/bin/python3 import httplib2 http = httplib2.Http() content = http.request("http://localhost/agent.php", method="GET", headers={'user-agent': 'Python script'})[1] print(content.decode()) ``` 該腳本為`agent.php`腳本創建一個簡單的 GET 請求。 在`headers`詞典中,我們指定用戶代理。 這可以通過 PHP 腳本讀取,并返回給客戶端。 ```py $ ./user_agent.py Python script ``` 服務器使用我們隨請求發送的代理名稱進行了響應。 ## 將用戶名/密碼添加到請求 客戶端的`add_credentials()`方法設置用于領域的名稱和密碼。 安全領域是一種用于保護 Web 應用資源的機制。 ```py $ sudo apt-get install apache2-utils $ sudo htpasswd -c /etc/nginx/.htpasswd user7 New password: Re-type new password: Adding password for user user7 ``` 我們使用`htpasswd`工具創建用于基本 HTTP 認證的用戶名和密碼。 ```py location /secure { auth_basic "Restricted Area"; auth_basic_user_file /etc/nginx/.htpasswd; } ``` 在 nginx `/etc/nginx/sites-available/default`配置文件中,我們創建一個安全頁面。 領域的名稱是“禁區”。 ```py <!DOCTYPE html> <html lang="en"> <head> <title>Secure page</title> </head> <body> <p> This is a secure page. </p> </body> </html> ``` 在`/usr/share/nginx/html/secure`目錄中,我們具有上面的 HTML 文件。 ```py #!/usr/bin/python3 import httplib2 user = 'user7' passwd = '7user' http = httplib2.Http() http.add_credentials(user, passwd) content = http.request("http://localhost/secure/")[1] print(content.decode()) ``` 該腳本連接到安全網頁; 它提供訪問該頁面所需的用戶名和密碼。 ```py $ ./credentials.py <!DOCTYPE html> <html lang="en"> <head> <title>Secure page</title> </head> <body> <p> This is a secure page. </p> </body> </html> ``` 使用正確的憑據,腳本將返回受保護的頁面。 在本教程中,我們探索了 Python `httplib2`模塊。 該教程由 Jan Bodnar 編寫,他經營著 [zetcode.com](http://zetcode.com),后者專門研究編程教程。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看