프로그래밍/Python 25

파이썬 전역 변수

파이썬에서 전역변수 사용하기 (2.7X 버전) 조대협 (http://bcho.tistory.com) 파이썬에서 전역 변수를 사용하려고 하니 "Unbound Local Error"가 나더라.파이썬은 로컬 변수를 자바처럼 쓸수가 없다. 잘못된 코드 global_value = 1 def myfunction(): global_value=global_value + 1 올바른 코드 global_value = 1 def myfunction(): global global_value global_value=global_value + 1 글로벌 변수로 쓰려면, 글로벌 변수를 쓰려는 곳에서 global 이라는 키워드로 선언을 해줘야 그 전역 변수를 불러다가 쓸 수 있다.

django 에서 REST API 만들기

Dango에서 간단한 REST API 만들기 조대협 Django에서 REST API를 만들려면 가장 널리 사용되는 프레임웍중 하나가 dango rest_framework이다.http://django-rest-framework.org/tutorial/quickstart 설치는 다음과 같다.pip install djangorestframeworkpip install markdown # Markdown support for the browsable API.pip install django-filter # Filtering support 1. quickstart라는 앱을 생성 C:\Users\terry\git\django_restframework_sample\django_restframework_sample>p..

Python에서 Open API 호출하기

Rest API를 호출하기여러가지 라이브러리 (urllib2, httplib2)등을 체크해봤으나, https 를 가장 쉽게 호출할 수 있고, 사용하기 편한것은 requests라는 것이 가장 편리함 http://www.python-requests.org/en/latest/user/quickstart/#make-a-request 에서 curl 로 다운로드 하고 설치해서 사용 위는 도스창을 이용해서 간단하게 https로 dna.daum.net을 호출한 코드인데, 리턴값이 한글이라서 그런지. cp949 encode 에러가 남. (이건 나중에 수정해야 할거 같고) 기타 참고 자료 (아래) 참고 : API 호출 하기https://dna.daum.net/tools/python/tutorial SSL 사용하기urlli..

Django에서 static file (css,img 사용하기)

django는 모듈화가 잘된건지. 웹개발에 최적화 된건지여하간, tomcat처럼 디렉토리에 이미지나 CSS를 넣는다고 찾아지지 않는다. (html) templates 처럼 특정 디렉토리를 생성하고 setting.py 파일에 지정해줘야 한다 setting.py에서STATIC_URL = '/static/' 해주고 각 app 디렉토리 밑에 /static이란 디렉토리를 만들어준다.사용할 때는 load staticfiles를 불러준후에 경로를 다음과 같이 지정해주면 된다. {% load staticfiles %}this is group home ※ static file 경로 지정 방법 -https://docs.djangoproject.com/en/1.6/howto/static-files/

Django Template

Django의 MVC 구조 원본- littlegreenriver.comTemplate 란? Template은 쉽게 이야기 하면 jsp 파일과 같다고 보면된다.template은 string으로 파일에서 로딩할 수 도 있고 몇개의 notation을 통해서 구성된다.마치 JSP의 처럼time.html 파일이 다음과 같을때 current time is {{ current_time }} 이는 current time is (JSP에서)와 같은 의미라고 보면된다.실제 view 모듈에서 이 템플릿을 렌더링 하는데, 이때, tempalte에 있는 인자들을 대입한다. template = loader.get_template('time.html') current_time = datetime.datetime.now() cont..

Django Hello World

http://www.djangobook.com/en/2.0/chapter03.htmlDjango 설치후 django-admin.py startprojec Yurryt로 사이트 만들고 (Yurry 라는 프로젝트가 만들어짐)※ 참고 : Yurry 디렉토리안에는 urls.py, settings.py,_init_.py 등의 파일이 들어 있음. ../Yurry에 manage.py 파일등이 들어 있음Yurry/views.py 라는 파일을 만듬from django.http import HttpResponse def hello(request): return HttpResponse("Hello world")간단하게 Hello World를 Print Out하는 코드그 다음 Yurry/urls.py에from django.c..

초경량 Python 웹서버 bottle을 이용한 MVC 예제 + Cookie

초경량 Python 웹서버 bottleMVC 예제 Controller 파일 구현 /controller.py import bottle mythings = ['apple','orange','banana','peach'] @bottle.route('/') def home_page(): fruit = bottle.request.get_cookie("fruit") return bottle.template("hello_world",username="Andrew",things=mythings,like=fruit) @bottle.post('/favorite_fruits') def favorite_fruits(): fruit = bottle.request.forms.get('fruit') if(fruit == None ..

Python 경량 웹서버 Bottle 를 Win7에 설치에서 실행까지

Bottle은 Python 기반의 초경량 웹서버이다.Python 쪽에서는 Django등을 많이 사용하고 있지만, 설치도 쉽고, 가볍고 해서, 특정 목적이나 가벼운 테스트 웹 애플리케이션 개발등에는 충분히 활용이 가능하다.(자바의 Jetty 와 같은 느낌?) 1. easy_installer에 설치먼저 bottle을 설치하려면, installer부터 설치하는 것이 수월하다. python은 linux의 yum이나 rpm , 또는 Java의 maven과 유사하게 Pypi(Python Package Index - https://pypi.python.org/pypi ) 라는 툴을 제공한다. 먼저 https://pypi.python.org/pypi/setuptools#windows 에서 ez_setup.py를 다운로..