1

私は最近 Python を始めました。JSON 文字列をエンコードし、それを HTTP リクエストとして URL に送信し、レスポンスを解析する方法についてのコードを投稿していただけないでしょうか。

ここに私が遊んでいるいくつかのコードがあります:

import os
import json

if os.name == 'nt':
    def clear_console():
        subprocess.call("cls", shell=True)
        return
else:
    def clear_console():
        subprocess.call("clear", shell=True)
        return

def login_call(username, password):

choice = 0

while int(choice) not in range(1,2):
    clear_console()
    print ('')
    print ('  Json Calls - Menu')
    choice = input('''
  1. Login.

  Enter Option: ''')

print ('')

choice = int(choice)

if choice == 1:

    login_call(username, password)
4

2 に答える 2

4

先日、github api に対してそのようなことを行うための回答を書きました。ここで私の答えを見てください。

本質的に、コードは次のように要約されます。

import urllib2
import json


data = {"text": "Hello world github/linguist#1 **cool**, and #1!"}
json_data = json.dumps(data)

req = urllib2.Request("https://api.github.com/markdown")
result = urllib2.urlopen(req, json_data)

print '\n'.join(result.readlines())
于 2012-07-30T22:09:56.317 に答える
1

必要なモジュールは、Python のバージョンに応じてhttplibまたはhttp.client、およびjsonです。JSON では、simple関数loadsdumps関数によって、JSON を簡単にエンコードおよびデコードする必要があります。

于 2012-07-30T22:09:45.633 に答える