[ socket ]
from socket import socket, AF_INET, SOCK_STREAM host = '192.168.0.10' port = 80 request = 'data' s = socket(AF_INET, SOCK_STREAM) s.connect((host, port)) s.send(request) response = s.recv(1000) s.close()
[ urllib2 ]
import urllib, urllib2
from urlgrabber.keepalive import HTTPHandler
url = "http://testurl"
######### GET #########
req = urllib2.Request(url)
######### POST #########
data = {'id':'TestID', 'pw':'TestPW'}
data = urllib.urlencode(data)
req = urllib2.Request(url, data)
######## Header data #######
user_agent = 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36'
cookie = 'abc'
req.add_header('User-agent', user_agent)
req.add_header('cookie', cookie)
####### Keep Alive #######
keepalive_handler = HTTPHandler()
opener = urllib2.build_opener(keepalive_handler)
opener.addheaders[0] = ('User-agnet', user_agent)
#req.add_header('User-agent', user_agent)도 하면 중복된다.
#opener.addheaders에 리스트 형태로 [('User-agent', 'Python-urllib/2.7')]가 저장되어 있음
urllib2.install_opener(opener)
####### Proxy Setting #######
proxy = urllib2.ProxyHandler({'http': '127.0.0.1:8080'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
#############################
response = urllib2.urlopen(req)
header = response.headers.headers
data = response.read()
User-agent : http://www.useragentstring.com/pages/useragentstring.php
Proxy-list site : http://gatherproxy.com/
urlgrabber : https://pypi.python.org/pypi/urlgrabber/3.1.0
[ httplib ]
import httplib
import urllib
url = 'www.test.com'
conn = httplib.HTTPConnection(url)
######### GET #########
conn.request('GET', '/index.html')
######### POST #########
post_param = urllib.urlencode({'id':'test', 'pw':'test'})
headers = {'Cookie':'abcd'}
conn.request('POST', '/index.html', post_param, headers)
#############################
response = conn.getresponse()
print response.status
print response.reason
print response.getheaders()
print response.read()
urllib의 경우에는 user-agent 값이 디폴트로 "User-Agent: Python-urllib/1.17"가 되고 httplib는 따로 세팅해주지 않으면 존재하지 않는다.
'Programming > Python' 카테고리의 다른 글
| python HTML, XML 파싱 - BeautifulSoup (0) | 2013.11.21 |
|---|---|
| python SOCKS Proxy 사용 - socksipy (0) | 2013.11.08 |
| Python Editor (0) | 2013.10.31 |
| python 콜스택 추적 (2) | 2013.10.09 |
| 파이썬 - 입력받기(input, raw_input) (3) | 2012.09.17 |