python pycurl 사용

2433 단어 python
urllib 를 사용 할 때 자주 죽 습 니 다. 예전 에 debug 는 timing out 을 설정 하지 않 았 기 때문에 시간 을 초과 하면 죽 습 니 다.
Pyc URL 은 curl 의 python 라 이브 러 리 입 니 다. 일부 curl 의 기능 은 실현 되 지 않 았 지만 강력 합 니 다.
curl 은 매우 강력 한 도구 입 니 다.
Google 내부 에 서 는 GDATA API 를 디버그 합 니 다. cURL 을 사용 하여 Google 데이터 서비스 와 상호 작용 합 니 다.
갈 수 있다http://pycurl.sourceforge.net/ 최신 Pyc URL 을 다운로드 합 니 다.
간단 한 Pyc URL 예
import pycurl
import StringIO
 
url = "http://www.google.com/"
crl = pycurl.Curl()
crl.setopt(pycurl.VERBOSE,1)
crl.setopt(pycurl.FOLLOWLOCATION, 1)
crl.setopt(pycurl.MAXREDIRS, 5)
crl.fp = StringIO.StringIO()
crl.setopt(pycurl.URL, url)
crl.setopt(crl.WRITEFUNCTION, crl.fp.write)
crl.perform()
print crl.fp.getvalue()



PycURL 쿠키 자동 처리
import pycurl
import StringIO
 
url = "http://www.google.com/"
crl = pycurl.Curl()
crl.setopt(pycurl.VERBOSE,1)
crl.setopt(pycurl.FOLLOWLOCATION, 1)
crl.setopt(pycurl.MAXREDIRS, 5)
crl.fp = StringIO.StringIO()
crl.setopt(pycurl.URL, url)
crl.setopt(crl.WRITEFUNCTION, crl.fp.write)
 
# Option -b/--cookie <name=string/file> Cookie string or file to read cookies from
# Note: must be a string, not a file object.
crl.setopt(pycurl.COOKIEFILE, "cookie_file_name")
 
# Option -c/--cookie-jar <file> Write cookies to this file after operation
# Note: must be a string, not a file object.
crl.setopt(pycurl.COOKIEJAR, "cookie_file_name")
 
crl.perform()
print crl.fp.getvalue()

Pyc URL POST 구현 방법
import pycurl
import StringIO
import urllib
 
url = "http://www.google.com/"
post_data_dic = {"name":"value"}
crl = pycurl.Curl()
crl.setopt(pycurl.VERBOSE,1)
crl.setopt(pycurl.FOLLOWLOCATION, 1)
crl.setopt(pycurl.MAXREDIRS, 5)
#crl.setopt(pycurl.AUTOREFERER,1)
 
crl.setopt(pycurl.CONNECTTIMEOUT, 60)
crl.setopt(pycurl.TIMEOUT, 300)
#crl.setopt(pycurl.PROXY,proxy)
crl.setopt(pycurl.HTTPPROXYTUNNEL,1)
#crl.setopt(pycurl.NOSIGNAL, 1)
crl.fp = StringIO.StringIO()
crl.setopt(pycurl.USERAGENT, "dhgu hoho")
 
# Option -d/--data <data>   HTTP POST data
crl.setopt(crl.POSTFIELDS,  urllib.urlencode(post_data_dic))
 
crl.setopt(pycurl.URL, url)
crl.setopt(crl.WRITEFUNCTION, crl.fp.write)
crl.perform()
 
print crl.fp.getvalue()

urllib 시간 초과 설정
import socket
socket.setdefaulttimeout(5.0)

좋은 웹페이지 즐겨찾기