GET 방식

import urllib2 # 1. 일반적 GET으로 호출 하는 방법 (url ? query string으로 날릴시) response = urllib2.urlopen('http://test.com?param1=a&param2=b') # 2. header 의 값이 필요 할때 request = urllib2.Request('http://test.com?param1=a&param2=b', None, {'Content-Type': 'application/json'}) response = urllib2.urlopen(request) # 3. 2번과 동일 하나 명시적으로 파라미터를 호출 request = urllib2.Request('http://test.com?param1=a&param2=b', headers={'Content-Type': 'application/json'}) response = urllib2.urlopen(request)


POST 방식

import urllib2
import urllib
# POST 요청은 data 값이 들어 있는가에 따라 요청 방식이 달라진다.
data = '{"param1":"a", "param2" : "b"}'
data = urllib.urlencode(data)
request = urllib2.Request('http://test.com', data, xxxx)
response = urllib2.urlopen(request)


차이점은 urllib2.Request() 함수에 url 인자에 파라미터를 전부 보내느냐  <- GET

2번째 인자로 파라미터 딕셔너리로 보낼 것이냐 차이. <- POST

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import urllib
import urllib2
import time
 
url = "http://webhacking.kr"
id_pw = {'id' : 'rap1er''pw' : 'password'}
user_agent = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'}
session_id = 'PHPSESSID=3da1bb25110cf6a6985794933b3bf4e8'
print session_id
 
#target_url = url + '/index.php'
 
while True:
    req = urllib2.Request(url, urllib.urlencode(id_pw), headers=user_agent)
    req.add_header('cookie', session_id)
    res = urllib2.urlopen(req)
    print res.read()
 
    time.sleep(180)    
cs


문제 풀라고 고민하다가 자꾸 세션이 만료돼서 로그인을 다시해주는게 귀찮아서 만듬

+ Recent posts