2019년 2월 23일 토요일

[Python] HTTP Example


1. HTTP Get Client

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import http.client, urllib.parse

def httpGet(url):
    conn = http.client.HTTPSConnection(url)
    conn.request("GET", "/")
    rv = conn.getresponse()
    data = None
    if rv.status == 200 or rv.reason == "OK":
        data = rv.read()
    return rv, data

def httpProxyGet(proxyaddr, proxyport, url):
    conn = http.client.HTTPSConnection(proxyaddr, proxyport)
    conn.set_tunnel(url)
    conn.request("GET", "/")
    rv = conn.getresponse()
    data = None
    if rv.status == 200 or rv.reason == "OK":
        data = rv.read()
    conn.close()
    return rv, data

def httpGetTest():
    rv, data = httpGet("www.python.org")
    print( data )
    print( rv.status, rv.reason )
    
if __name__ == "__main__":
    httpGetTest()


2. HTTP Post


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def httpPost(url):
    params = urllib.parse.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    conn = http.client.HTTPConnection(url)
    conn.request("POST", "", params, headers)
    rv = conn.getresponse()
    data = None
    if rv.status == 200 or rv.reason == "OK":
        data = rv.read()
    conn.close()
    return rv, data




댓글 없음:

댓글 쓰기