Python Interface Test (iii)

Source: Internet
Author: User

Four: Python http of the interface Request

The power of Python is that it provides a lot of standard libraries and third libraries, and this article introduces Urllib

and the third library of requests.

Urllib defines a number of functions and classes that can help us get URL content in complex situations. Complex situations-basic and in-depth verification, redirection, cookies, etc.

The GET request code for Urllib is as follows:

Import urllib.requesturl= ' http://www.baidu.com ' response=urllib.request.request (url=url) html= Urllib.request.urlopen (response) print (Html.getcode ()) print (html.headers)

Request Result:

/library/frameworks/python.framework/versions/3.4/bin/python3.4/users/playcrab/pycharmprojects/jiekouceshi/ pachong.py

200

Date:mon, 08:08:36 GMT

content-type:text/html; Charset=utf-8

Transfer-encoding:chunked

Connection:close

set-cookie:baiduid=d3e5547acc26d3908ebb29522babccd4:fg=1; Expires=thu, 31-dec-37 23:55:55 GMT; max-age=2147483647; path=/; Domain=.baidu.com

SET-COOKIE:BIDUPSID=D3E5547ACC26D3908EBB29522BABCCD4; Expires=thu, 31-dec-37 23:55:55 GMT; max-age=2147483647; path=/; Domain=.baidu.com

set-cookie:pstm=1487578116; Expires=thu, 31-dec-37 23:55:55 GMT; max-age=2147483647; path=/; Domain=.baidu.com

set-cookie:bdsvrtm=0; path=/

set-cookie:bd_home=0; path=/

set-cookie:h_ps_pssid=21935_1455_21090_17001_22036; path=/; Domain=.baidu.com

p3p:cp= "OTI DSP COR IVA our IND COM"

Cache-control:private

cxy_all:baidu+547441cee80f5b2514d2439b86d8b151

Expires:mon, 08:08:33 GMT

x-powered-by:hphp

server:bws/1.1

X-ua-compatible:ie=edge,chrome=1

Bdpagetype:1

bdqid:0xc3d33f280001f43e

bduserid:0

Urllib POST request, code:

Import urllib.requestimport urllib.parseurl= ' Http://www.tuling123.com/openapi/api ' data={"key": "Your", "info": ' Hello ' }data=urllib.parse.urlencode (data) encode (' Utf-8 ') re=urllib.request.request (url,data) html= Urllib.request.urlopen (re) print (Html.getcode (), html.msg) print (Html.read ())

Results:

/library/frameworks/python.framework/versions/3.4/bin/python3.4/users/playcrab/pycharmprojects/jiekouceshi/ pachong.py

OK

B ' {"Code": 40001, "text": "\xe4\xba\xb2\xe7\x88\xb1\xe7\x9a\x84\xef\xbc\x8ckey\xe4\xb8\x8d\xe5\xaf\xb9\xe5\x93\ Xa6\xe3\x80\x82 "}"

(Note: This is not garbled is the output format of the problem.) )

The following describes the HTTP requests for the requests library,

GET Request:

Import REQUESTSR = Requests.get (' https://www.baidu.com ') print (r.headers)

Results:

Results:

/library/frameworks/python.framework/versions/3.4/bin/python3.4/users/playcrab/pycharmprojects/jiekouceshi/ pachong.py

OK

B ' {"Code": 40001, "text": "\xe4\xba\xb2\xe7\x88\xb1\xe7\x9a\x84\xef\xbc\x8ckey\xe4\xb8\x8d\xe5\xaf\xb9\xe5\x93\ Xa6\xe3\x80\x82 "}"

(Note: This is not garbled is the output format of the problem.) )

The following describes the HTTP requests for the requests library,

GET Request:

Import REQUESTSR = Requests.get (' https://www.baidu.com ') print (r.headers)

Results:

{' last-modified ': ' Mon, 13:23:55 GMT ', ' transfer-encoding ': ' chunked ', ' Pragma ': ' No-cache ', ' Cache-control ': ' Private, No-cache, No-store, Proxy-revalidate, No-transform ', ' content-encoding ': ' gzip ', ' Date ': ' Mon, Feb 2017 08:3 0:29 GMT ', ' Server ': ' bfe/1.0.8.18 ', ' Connection ': ' keep-alive ', ' content-type ': ' text/html ', ' set-cookie ': ' bdorz= 27315; max-age=86400; domain=.baidu.com; path=/, __bsi=12827760870170119103_00_7_n_n_2_0301_002f_n_n_n_0; Expires=mon, 20-feb-17 08:30:34 GMT; domain=www.baidu.com; path=/'}

POST request:

Import requestspayload = {' Key1 ': ' value1 ', ' key2 ': ' value2 '}r = Requests.post ("Http://httpbin.org/post", Data=payload) Print (R.text)

@font-face {font-family: "Times";} @font-face {font-family: "Song Body";} @font-face {font-family: "Cambria Math";} @font-face {font-family: "@ Song body";} @font-face {font-family: "Cambria";} @font-face {font-family: "Menlo";} P.msonormal, Li. MsoNormal, Div. MsoNormal {margin:0cm 0cm 0.0001pt; text-align:justify; font-size:12pt; font-family:cambria;} p {margin-right:0cm; margin-left:0cm; font-size:10pt; font-family:times;} Pre {margin:0cm 0cm 0.0001pt; font-size:10pt; font-family:courier;} Span. HTML {font-family:courier;}. Msochpdefault {font-size:10pt; font-family:cambria;} Div. WordSection1 {}

Results:
{
"Form": {
"Key2": "value2"
"Headers": {
"User-agent": "python-requests/2.10.0"
"url": "Http://httpbin.org/post"

The above is a case of sending get and post requests using urllib and requests.

Python Interface Test (c)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.