Python 访问Google+ (http),pythongoogle


CODE:

#!/usr/bin/python 
# -*- coding: utf-8 -*-

'''
Created on 2014-8-28
@author: guaguastd
@name: login.py
'''

# Request over http
def google_login_http(resource_type, action, field):
    
    # import requests
    import requests
    import re
    
    # key information
    key = ''
    
    # base url
    url = 'https://www.googleapis.com/plus/v1'
    
    # build standard field
    for matStr,repStr in [[" ", "+"], ["'", "%27"]]:
        reobj = re.compile(matStr)
        field = reobj.sub(repStr, field)
        
    # Build the urls
    urls = '%s/%s?%s=%s&alt=json&key=%s' % (url, resource_type, action, field, key)
    print 'urls: ', urls
    
    # Get the content
    content = requests.get(urls, verify=False).json()
    
    # return
    return content

#!/usr/bin/python 
# -*- coding: utf-8 -*-

'''
Created on 2014-8-22
@author: guaguastd
@name: search_for_person.py
'''

import json

#import login
from login import google_login_http

while True:
    query = raw_input("Input query(None to quit): ")
    if query is None:
        break
    
    people_feed = google_login_http("people", "query", query)
    #people_feed = google_api.people().search(query=query).execute()
    print json.dumps(people_feed['items'], indent=1)

RESULT:

Input query(None to quit): Tim O'Reilly
[
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/+TimOReilly", 
  "image": {
   "url": "https://lh4.googleusercontent.com/-J8nmMwIhpiA/AAAAAAAAAAI/AAAAAAACoLI/_eGkAvDl1Lg/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/gVEWOJe4zmELXPIOBGB2zJT77YY\"", 
  "id": "107033731246200681024", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/112971313488103174611", 
  "image": {
   "url": "https://lh4.googleusercontent.com/-DHPPpI53zYs/AAAAAAAAAAI/AAAAAAAAAA0/pMnv4IOHinM/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/-tQMA-kdju1h40Q9yqHvt_XGcI8\"", 
  "id": "112971313488103174611", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/115665711705516993369", 
  "image": {
   "url": "https://lh3.googleusercontent.com/-ykapO9VSsmU/AAAAAAAAAAI/AAAAAAAAAGE/QjNpGPy5SIc/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/0z-EwRK70ma_SgL89TnXDnNCeg4\"", 
  "id": "115665711705516993369", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "TIM O'REILLY", 
  "url": "https://plus.google.com/112630513029834463088", 
  "image": {
   "url": "https://lh3.googleusercontent.com/-dOC3TnU5LJk/AAAAAAAAAAI/AAAAAAAACXA/zxEC3Q2samc/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/kcitEq1pVj3GQMicyWbxqUM9vXc\"", 
  "id": "112630513029834463088", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/117480952841562724952", 
  "image": {
   "url": "https://lh6.googleusercontent.com/-bpWGUQXAWg0/AAAAAAAAAAI/AAAAAAAAAIs/fCBaH6ldLXU/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/fbrFj4lJ_cpWx9pUUmO6476cZtY\"", 
  "id": "117480952841562724952", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/103336854217201973281", 
  "image": {
   "url": "https://lh4.googleusercontent.com/-J3iV2F3ex4E/AAAAAAAAAAI/AAAAAAAAAIA/DJKY_V0epG8/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/0dlnI0n2qoODg63r8NG6Xobol8I\"", 
  "id": "103336854217201973281", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/102467783732787713430", 
  "image": {
   "url": "https://lh4.googleusercontent.com/-P2nQYJTUZY8/AAAAAAAAAAI/AAAAAAAAAAA/b1obv1Rgmg4/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/H3_JCPzhKdR_ZptxJRHGswnL018\"", 
  "id": "102467783732787713430", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Timothy O'Reilly", 
  "url": "https://plus.google.com/+TimothyOReilly", 
  "image": {
   "url": "https://lh3.googleusercontent.com/-TdJsE6Qsxzg/AAAAAAAAAAI/AAAAAAAAAis/4ltdyyk61mQ/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/3744uM_v9M6mz9icJoOhqz08cPs\"", 
  "id": "107415629896108700526", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/106492472890341598734", 
  "image": {
   "url": "https://lh3.googleusercontent.com/-S5DUz4QdL4k/AAAAAAAAAAI/AAAAAAAAAMU/_MARUPifxP4/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/3mHGPwEpTqJTOYMLD8ZTGXAFHxE\"", 
  "id": "106492472890341598734", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Timothy O'Reilly", 
  "url": "https://plus.google.com/104189405442379396369", 
  "image": {
   "url": "https://lh4.googleusercontent.com/-JY0W6EiqN6k/AAAAAAAAAAI/AAAAAAAAD8I/ecdZSHwsiQk/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/YxUJ7XitqA_et4VyLmje5-UZbGM\"", 
  "id": "104189405442379396369", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/106680419484587025186", 
  "image": {
   "url": "https://lh3.googleusercontent.com/-5wQu1d4wF2w/AAAAAAAAAAI/AAAAAAAAAKA/OqGwRWaeVFI/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/-a3tNtNGvyUhChUVfGHHROC16Zo\"", 
  "id": "106680419484587025186", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "TIM O'REILLY", 
  "url": "https://plus.google.com/110160587587635791009", 
  "image": {
   "url": "https://lh4.googleusercontent.com/-gWq9vr_JEnc/AAAAAAAAAAI/AAAAAAAAADI/zwCXKP4QeiU/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/ofg-30rIv-rKw7XTBBnDA1i3I_Y\"", 
  "id": "110160587587635791009", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/116668755775044955466", 
  "image": {
   "url": "https://lh5.googleusercontent.com/-vp_eOqn1RDo/AAAAAAAAAAI/AAAAAAAAAGw/kLYoiV8nPeY/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/ACiNfQOxMSKzzyGcb5jM5bmxl6I\"", 
  "id": "116668755775044955466", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Timothy O'Reilly", 
  "url": "https://plus.google.com/107786179585502589799", 
  "image": {
   "url": "https://lh6.googleusercontent.com/-VyqOCKcFBVg/AAAAAAAAAAI/AAAAAAAAACg/ccaDhAaSOrk/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/Dli96fXA5CodkDI-t66PRnrQFJU\"", 
  "id": "107786179585502589799", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Timothy O'Reilly", 
  "url": "https://plus.google.com/102994447097932477991", 
  "image": {
   "url": "https://lh6.googleusercontent.com/-AN65SwKBkAA/AAAAAAAAAAI/AAAAAAAAAFI/oYk5S6oRSRM/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/ZGOPPF5KIorb7COpKZ97WBd0bNc\"", 
  "id": "102994447097932477991", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/103354252176309484961", 
  "image": {
   "url": "https://lh5.googleusercontent.com/-_isKE1mWRt0/AAAAAAAAAAI/AAAAAAAAABM/36dDkRiYvBk/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/GR12dQs7rTeDOnnHnSZOHFC42Ao\"", 
  "id": "103354252176309484961", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Timothy O'Reilly", 
  "url": "https://plus.google.com/110507132467773975582", 
  "image": {
   "url": "https://lh5.googleusercontent.com/-tDB-zaAAuD0/AAAAAAAAAAI/AAAAAAAAACY/48R4BFSJ-XQ/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/rgBrNe4FFc6FOl45g_dcf8Cx5ag\"", 
  "id": "110507132467773975582", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/114402115097637564691", 
  "image": {
   "url": "https://lh5.googleusercontent.com/-ur8ek-VIzeE/AAAAAAAAAAI/AAAAAAAAAMw/JboA3pGeGM4/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/UIRX0od1spatTgp8b_Et5uyxpXM\"", 
  "id": "114402115097637564691", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/100454277852249756307", 
  "image": {
   "url": "https://lh6.googleusercontent.com/-wYYn4xPXt-s/AAAAAAAAAAI/AAAAAAAAACQ/rXE2GVPnCTM/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/JIKcTr6TRO2TcSlLy6jl_kZ68yA\"", 
  "id": "100454277852249756307", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/103096937174083026967", 
  "image": {
   "url": "https://lh3.googleusercontent.com/-gVHQA9qDFQ4/AAAAAAAAAAI/AAAAAAAAAAA/wEAfdMcxEX4/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/JupbCFJJqLSOZYsnRCbDMibJ308\"", 
  "id": "103096937174083026967", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Tim O'Reilly", 
  "url": "https://plus.google.com/114172069562252200235", 
  "image": {
   "url": "https://lh5.googleusercontent.com/-3xPm1RmotzY/AAAAAAAAAAI/AAAAAAAAAC0/_Ue0ROpAMJk/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/mgTUB4vuFDnN35OV-TfovLuk42c\"", 
  "id": "114172069562252200235", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Timothy O'Reilly", 
  "url": "https://plus.google.com/101557961285050424605", 
  "image": {
   "url": "https://lh5.googleusercontent.com/-lTaqhOsfcxw/AAAAAAAAAAI/AAAAAAAAEHM/FLiBXUD0-bk/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/cfMaCQrodj65T59aCWRTZDG1XGU\"", 
  "id": "101557961285050424605", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Timothy O'Reilly", 
  "url": "https://plus.google.com/113556773460517050463", 
  "image": {
   "url": "https://lh6.googleusercontent.com/-u22u7FNC4Ak/AAAAAAAAAAI/AAAAAAAAAHE/LjpQ7kqjL2g/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/CGEFZv3-qj3jyGLoz4x7MCBUsp4\"", 
  "id": "113556773460517050463", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Timothy O'Reilly", 
  "url": "https://plus.google.com/109370454456340755312", 
  "image": {
   "url": "https://lh4.googleusercontent.com/-5WtmN0-NptY/AAAAAAAAAAI/AAAAAAAAABM/UrEmKNnkczk/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/kvne_6PcngEbH44gXZT5c5_aewc\"", 
  "id": "109370454456340755312", 
  "objectType": "person"
 }, 
 {
  "kind": "plus#person", 
  "displayName": "Timothy O'Reilly", 
  "url": "https://plus.google.com/106943606032560183935", 
  "image": {
   "url": "https://lh3.googleusercontent.com/-W6NwjvPOcR8/AAAAAAAAAAI/AAAAAAAAABM/2dcYJDcixcQ/photo.jpg?sz=50"
  }, 
  "etag": "\"pNz5TVTpPz2Rn5Xw8UrubkkbOJ0/WYHhEYNT2FuI9bHkE_5ZyhwF7Q0\"", 
  "id": "106943606032560183935", 
  "objectType": "person"
 }
]
Input query(None to quit): 



怎在google app engine上运行一个简单python程序?

看google的引导,很简单的。
code.google.com/...w.html
 

python http时使用GET返回成功,使用POST却返回失败

你访问的服务器返回的post信息无法识别,可能你的代理服务器拒绝post头信息,你在别的网路试试你的代码可能不会报错。你可以在res = con.getresponse()前面添加一句conn.set_debuglevel(1)来看看具体的信息。
以下来自 documentation for httplib:

exception httplib.BadStatusLine
A subclass of HTTPException.Raisedif a server responds with a HTTP status code that we don’t understand
 

相关内容