Bootstrap

python post请求实例_python的post请求实例

#coding=utf-8

import json

import urllib2

import base64

import sys

# start=sys.argv[1]

# end=sys.argv[2]

# DEFAULT_URL =""

DEFAULT_URL =""

auth = base64.b64encode("admin" + ':' + "admin")

for i in range(1):

body = [{

"Tag":"hhhuuuuuuu",

"Type":"rr"

}]

# body = {"name":"hhnnnnnnnnnnnnnnnnnn","desc":"bbbbbbbbbbbbbbbbbb"}

print(type(body))

j_data = json.dumps(body)

print(type(j_data))

print(j_data)

headers = {

"Authorization": "Basic " + auth,

"Accepty":"application/json, text/plain, */*",

"Accept-Encoding": "gzip, deflate",

"Accept-Language":"zh-CN,zh;q=0.9",

"Connection":"keep-alive",

"Content-Length":"363",

"Content-Type":"application/json;charset=UTF-8",

"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",

"X-Requested-By":"Data Collector"

}

post_request = urllib2.Request(DEFAULT_URL, data=j_data, headers=headers)

# post_request.add_header( "Authorization", "Basic " + auth)

#post_request.get_method = lambda: 'POST'

mydict=post_request.__dict__

print mydict

try:

post_response = urllib2.urlopen(post_request)

except urllib2.HTTPError, e:

print e.read()

# get_request = urllib2.Request(DEFAULT_URL)

# get_request.add_header( "Authorization", "Basic " + auth)

# get_request.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36")

# get_request.get_method = lambda: 'GET'

# get_response = urllib2.urlopen(get_request)

# result =get_response.read()

# print(result)

;