origoni's Blog from Millky

origoni의 스프링 블로그 입니다.

webkit2png 설치 및 Flask로 웹 서비스 만들기

이번엔 화면을 저장해야 할 이슈가 있어서 여러가지를 찾아보던 중 역시 파이썬으로 만들어진 좋은 라이브러리를 발견했다~

파이썬에는 좋은 라이브러리가 많구나...


webkit2png


https://github.com/adamn/python-webkit2png


$ pip install webkit2png

$ wget https://raw.github.com/paulhammond/webkit2png/master/webkit2png

$ chmod a+x webkit2png

$ cd

$ mkdir python-webkit2png

$ git clone https://github.com/adamn/python-webkit2png.git python-webkit2png

$ python python-webkit2png/setup.py install

$ cd python-webkit2png/

$ python setup.py install

$ vi /etc/yum.repos.d/CentOS-ATrpms.repo

$ rpm --import http://packages.atrpms.net/RPM-GPG-KEY.atrpms

$ yum -y install qt47 qt47-devel qt47-webkit qt47-webkit-devel gcc gcc-c++

$ yum -y install xauth

$ cd /usr/local/src

$ wget http://sourceforge.net/projects/pyqt/files/sip/sip-4.16.2/sip-4.16.2.tar.gz

$ tar zxvf sip-4.16.2.tar.gz

$ cd sip-4.16.2

$ python configure.py

$ make && make install

$ cd /usr/local/src

$ wget http://effbot.org/downloads/Imaging-1.1.7.tar.gz

$ tar zxvf Imaging-1.1.7.tar.gz

$ cd Imaging-1.1.7

$ python setup.py install

$ cd /usr/local/src

$ wget http://downloads.sourceforge.net/project/pyqt/PyQt4/PyQt-4.11.1/PyQt-x11-gpl-4.11.1.tar.gz

$ tar zxvf PyQt-x11-gpl-4.11.1.tar.gz

$ cd PyQt-x11-gpl-4.11.1

$ python configure.py -q /usr/lib64/qt47/bin/qmake-qt47

$ make && make install

$ cd

$ cd python-webkit2png/

$ python scripts/webkit2png -h


아래의 문서들을 보고 설치 했다.

http://type.so/linux/centos6-webkit2png.html

http://alikian.me/2011/04/23/webkit2png-py-on-centos-5-5/



음 동작은 잘 하는것 같은데... 실행해보니...


$ python scripts/webkit2png http://millky.com

$ python scripts/webkit2png -x 1024 768 -g 1024 0 http://millky.com -o millky.png  


webkit2png: cannot connect to X server


이런 에러가 발생


http://stackoverflow.com/questions/6927217/error-cannot-connect-to-x-server-with-python-program-webkit2png



찾아보니 Xvfb라는것이 필요하네~


http://openwiki.kr/tech/xvfb

XVFB

Xvfb provides an X server that can run on machines with no display hardware and no physical input devices.

Xvfb가 Xorg를 대신하여 가상으로 X를 만들어 주는것


그래서 찾아보고 설치!!


$ yum -y install wqy-zenhei-fonts.noarch

$ yum -y install Xvfb

$ cd python-webkit2png/

$ xvfb-run -a -s "-screen 0 1024x768x16" python scripts/webkit2png  http://millky.com -o millky.png


참고 문서 들

http://eclipse4j.tistory.com/146

http://corpocrat.com/2008/08/19/how-to-install-xvfb-x11-server-in-linux-server/



그리고..

이전에 이(http://millky.com/home/byuri/10001029)와 마찬가지로 웹 서비스로 만들려 한다.


from flask import Flask, request, make_response, send_file
import subprocess
import os, urllib

app = Flask(__name__)
app.config['DEBUG'] = False

def img_response(filename):
    return send_file(filename, mimetype='image/png')

@app.route("/")
def get():
    url = request.args.get('url')
    if not url:
        return """
<!doctype html>
<html>
  <head>
    <title></title>
  </head>
  <body>
  </body>
</html>
"""
    else:
        fn = url.replace("http://","").replace(".","_").replace("/","-") + '.png'
        print fn
        xvfb_args = ['xvfb-run', '--server-args=-screen 0, 1024x768x24', '~/python-webkit2png/scripts/webkit2png', '%s' %url, '-o', '%s' %fn]
        process = subprocess.Popen(xvfb_args)
        return img_response(fn)

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 7000))
    app.run(host='0.0.0.0', port=port)


요리요리 하고 나니 결과는 잘 나오긴 하는데...
파일 생성되는 시간이 있어서. 위의 스크립트는 조금 수정해야 함.



origoni 2014-08-03 17:31:22

subprocess.Popen -> subprocess.call로 변경하면 된다~

origoni 2015-05-11 13:23:53

http://en.wikipedia.org/wiki/Xvfb

lay126 2015-07-16 00:48:34

안녕하세요, 클라우드 서버에서 테스트 중인 학생입니다. app.run(host='0.0.0.0', port=port) 이부분을 입력하고 서버를 켜도 클라우드 서버의 공인 ip주소로 접근이 되지 않습니다. 왜 그런걸까요 ㅠㅠ?
back to top