python post json

__author__ = 'andy'
#!/usr/bin/python3.2
# -*- coding: utf-8 -*-
from urllib.request import urlopen
import http.client
import json
import os
import time


baseUrl='http://solr/solr/collection_user/select?q=diploma:*+AND+NOT+diploma:(1+OR++2+OR+3+OR+4+OR+5+OR+6+OR+7+OR+8+OR+9+OR+10)+AND+flag:(s+OR+t)&wt=json&fl=userID&rows=%s&start=%s'
rows = 1000

def retrieveUsers():
    try:
        requestURL = baseUrl  % (0,0)
        print(requestURL)
        conn = urlopen(requestURL)
        response = eval(conn.read())
        numFound = response['response']['numFound']
        if numFound > 0:
            pages=int((numFound + rows - 1)/rows)
            f = open('updateUserids.txt','a+')
            for page in range(1,pages):
                requestURL=baseUrl % (rows, (page -1) * rows)
                print(requestURL)
                conn = urlopen(requestURL)
                response = eval(conn.read())
                for doc in response['response']['docs']:
                    f.write(str(doc['userID']) + '\n')
                f.flush()
                time.sleep(5)
            f.close()
    except:
        print('got error when request URL ' + requestURL + '\n')


def updateUsers():
    f = open('updateUserids.txt','r+')
    conn = http.client.HTTPConnection("xxx.xx.com")
    headers = {'Content-type':'application/json'}
    counter = 0
    useridlist = []
    for line in f:
        if not line.isspace():
            userID = line.strip('\n').strip()
            counter = counter + 1
            if counter % 100 == 0:
                try:
                    params = {"userIds":useridlist}
                    json_params = json.dumps(params)
                    print(json_params)
                    conn.request('POST', '/users/index/update', json_params, headers)
                    response = conn.getresponse()
                    data = response.read()
                    if response.status == 200:
                        print('success')
                    else:
                        print('fail')
                except:
                    print('got error when update users \n')
                finally:
                    del useridlist[:]
            else:
                useridlist.append(int(userID))
    conn.close()
    f.close()

retrieveUsers()
updateUsers()

 

 

simple one  from http://stackoverflow.com/questions/11763976/python-http-client-json-request-and-response-how

import http.client
import json

connection = http.client.HTTPSConnection('api.github.com')

headers = {'Content-type': 'application/json'}

foo = {'text': 'Hello world github/linguist#1 **cool**, and #1!'}
json_foo = json.dumps(foo)

connection.request('POST', '/markdown', json_foo, headers)

response = connection.getresponse()
print(response.read().decode())

 

 

posted on 2014-06-24 12:03  ukouryou  阅读(986)  评论(0编辑  收藏  举报

导航