Home >Backend Development >Python Tutorial >Detailed explanation of Python learning using urllib and urllib2 to access http GET/POST

Detailed explanation of Python learning using urllib and urllib2 to access http GET/POST

silencement
silencementforward
2019-11-25 15:45:022417browse

Detailed explanation of Python learning using urllib and urllib2 to access http GET/POST

Preface

This article mainly introduces how Python learns to access GET/POST of http. Using urllib and urllib2, you can easily implement http access. Visit, not much to say below, let’s take a look at the detailed introduction.

Detailed explanation of examples

The following is an example to implement GET and POST for http://127.0.0.1/cgi/test

uses query in the usual sense. string

POST accepts json

Recommended to study "Python Video Tutorial"

Among them, if the Request method of

urllib2 only takes One parameter is the GET method, but if it takes two parameters, it is the POST method of http, and the second parameter is the content of POST.

#!/usr/bin/env python

import urllib
import urllib2
import json

url_base = "http://127.0.0.1/ cgi/test"

#GET
try:
query = {'test':'yes','name':'colin'}
query_string = urllib.urlencode(query)
url = url_base "?" query_string
print "GET", url
print "web output:"
print urllib2.urlopen(urllib2.Request(url) ).read()
except Exception as err:
print err

#post
try:
url = url_base
print "POST", url
a = {'k1':123, 'k2': '456', 'k3':'test'}
json_s = json.dumps(a)
print "POST input:"
print json_s
print "web output:"
print urllib2.urlopen(urllib2.Request(url, json_s)).read()
except Exception as err:
print err

test is written in bash, as shown below, where jq is the command to process json, you need to download it, bash/sed/awk should be included in the system

#!/bin/bash
echo -e 'Content-type:text/plain\r'
echo -e '\r'

if [ X"$ REQUEST_METHOD" = X"POST" ];then
jq . | sed -nr '/:/!d; s/^([ \t]*"[^"] "[ \t]*):/\ 1=/;s/,[ \t]*$//;s/"//g;p'
else
echo ${QUERY_STRING} | awk 'BEGIN{RS="&"}1'
fi

You can use any webserver as long as it supports CGI. I am using a very lightweight webserver - boa. It may not be suitable for large application websites, but it is suitable for embedded applications. It is still very useful to provide APIs with CGI, and deployment is very easy.

After building, test it

$ ./test.py
GET http://127.0.0.1/v1/lic/test?test=yes&name=colin
web output:
test=yes
name=colin

POST http://127.0.0.1/v1/lic/test
POST input:
{"k3": "test ", "k2": "456", "k1": 123}
web output:
k3= test
k2= 456
k1= 123

Summary

The above is the entire content of this article. I hope that the content of this article has certain reference and learning value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.

The above is the detailed content of Detailed explanation of Python learning using urllib and urllib2 to access http GET/POST. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:www.word666.com. If there is any infringement, please contact admin@php.cn delete