1. 程式人生 > >網路與Web程式設計(一)

網路與Web程式設計(一)

  1. 作為客戶端與 HTTP 服務互動 // Problem 你需要通過 HTTP 協議以客戶端的方式訪問多種服務。例如,下載資料或者與基 於 REST 的 API 進行互動。 // Solution: urllib.request

傳送一個簡單的 HTTP GET 請求到遠端的服務上

from urllib import request, parse

# Base URL being accessed
url = 'http://httpbin.org/get'

# Dictionary of query parameters (if any)
parms = {
    'name1': 'value1'
, 'name2': 'value2', } # Encode the query string querystring = parse.urlencode(parms) # Make a GET request and read the response u = request.urlopen(url + '?' + querystring) resp = u.read() >>> { "args": { "name1": "value1", "name2": "value2" }, "headers": { "Accept-Encoding"
: "identity", "Connection": "close", "Host": "httpbin.org", "User-Agent": "Python-urllib/3.5" }, "origin": "117.136.38.38", "url": "http://httpbin.org/get?name2=value2&name1=value1" }

傳送一個簡單的 HTTP POST 請求到遠端的服務上

# 如果你需要使用 POST 方法在請求主體中傳送查詢引數,可以將引數編碼後作為
# 可選引數提供給 urlopen() 函式,
from urllib import request, parse # Base URL being accessed url = 'http://httpbin.org/post' # Dictionary of query parameters (if any) parms = { 'name1': 'value1', 'name2': 'value2' } # Encode the query string querystring = parse.urlencode(parms) # Make a POST request and read the response u = request.urlopen(url, querystring.encode('ascii')) resp = u.read() with open('1.txt', 'wb') as f: f.write(resp) >>> { "args": {}, "data": "", "files": {}, "form": { "name1": "value1", "name2": "value2" }, "headers": { "Accept-Encoding": "identity", "Connection": "close", "Content-Length": "25", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Python-urllib/3.5" }, "json": null, "origin": "117.136.38.38", "url": "http://httpbin.org/post" }

自定義的 HTTP 頭

如果你需要在發出的請求中提供一些自定義的 HTTP 頭,例如修改 user-agent 欄位, 可以建立一個包含欄位值的字典,並建立一個 Request 例項,然後將其傳給 urlopen()

from urllib import request, parse

url = "http://httpbin.org/post"

parms = {
    "name1": 'value1',
    "name2": 'value2'
}

# Extra headers
headers = {
    'User-agent': 'none/ofyourbusiness',
    'Spam': 'Eggs'
}

querystring = parse.urlencode(parms)

req = request.Request(url,
                      querystring.encode('ascii'), headers=headers)

# Make a request and read the response
u = request.urlopen(req)
resp = u.read()

with open("1.txt", 'wb') as file:
    file.write(resp)
>>>
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "name1": "value1", 
    "name2": "value2"
  }, 
  "headers": {
    "Accept-Encoding": "identity", 
    "Connection": "close", 
    "Content-Length": "25", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "Spam": "Eggs", 
    "User-Agent": "none/ofyourbusiness"
  }, 
  "json": null, 
  "origin": "117.136.38.38", 
  "url": "http://httpbin.org/post"
}

如果需要互動的服務比上面的例子都要複雜,requests 庫

import requests

url = "http://httpbin.org/post"

parms = {
    "name1": 'value1',
    "name2": 'value2'
}

# Extra headers
headers = {
    'User-agent': 'none/ofyourbusiness',
    'Spam': 'Eggs'
}

resp = requests.post(url, data=parms, headers=headers)

# Decoded text returned by the request
# resp.text 帶給我們的是以 Unicode 解碼的響應文字
# 如果去訪問 resp.content ,就會得到原始的二進位制資料。
# 如果訪問resp.json ,那麼就會得到 JSON 格式的響應內容
text = resp.text
with open("1.txt", 'wt') as file:
    file.write(text)
>>>
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "name1": "value1", 
    "name2": "value2"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "25", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "Spam": "Eggs", 
    "User-Agent": "none/ofyourbusiness"
  }, 
  "json": null, 
  "origin": "117.136.38.38", 
  "url": "http://httpbin.org/post"
}