2014년 11월 27일 목요일

Mac의 eclipse에 pydev3.x를 설치했는데도 메뉴에 나타나지 않는 문제

맥의 매버릭 이후 eclipse에 pydev3.x를 설치했으나 메뉴에 나타나지 않는 문제가 발생
JDK 1.7 설치로 해결



    1. Unistall PyDev
    2. Under Eclipse > preferences > Installed JREs you probably only see Java SE 6
    3. Download and install JDK 1.7 from http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
    4. open terminal and run "/usr/libexec/java_home -v 1.7"
      this will return the directory in which JDK 1.7 reside, something like /Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home
    5. Under Eclipse > preferences > Installed JREs click "add", select "MacOS X VM", click "next"
    6. in JRE Home paste your version of /Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home, give it a name and click "Finish"
    7. Restart Eclipse and re-install PyDev.
http://stackoverflow.com/a/23671271


JDK를 잘못 설치했을 경우

#cd /Library/Java/JavaVirtualMachines
#sudo rm -rf jdk1.7.0_06.jdk

2014년 11월 25일 화요일

CentOS + Apache + mod_wsgi + python + django + mysql


1.django 프레임웍 설치

$ git clone https://github.com/django/django.git
$ cd django
$ python setup.py install
$ python
>>> import django
>>> print django.get_version()
1.7

2. django 프로젝트 생성 및 설정

설치 후 생성된 django-admin.py를 프로젝트 생성을 원하는 폴더로 이동 후 프로젝트 생성

$ python django-admin.py startproject samplepro

프로젝트명을 samplepro로 하였다. 다음처럼 디렉토리가 구성된다.

samplepro/
      manage.py
      samplepro/
          __init__.py
          settings.py
          urls.py
          wsgi.py

생성된 프로젝트를 django 자체서버를 통해 테스트
$ cd samplepro
$ python manage.py runserver 
  Validating models...

  0 errors found
  Django version 1.7, using settings 'samplepro.settings'
  Development server is running at http://127.0.0.1:8000/

  Quit the server with CTRL-BREAK.

웹 브라우져로 http://127.0.0.1:8000/ 에서 웹페이지를 확인


mysql을 사용하기 위한 python 인터페이스(MySql_python) 설치.

$ yum install mysql-devel
$ pip install mysql_python

프로젝트의 settings.py파일에 mysql 접속정보 입력
$ vi settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'DB명',                       
        'USER': 'USER명',                 
        'PASSWORD': '비밀번호',
        'HOST': '127.0.0.1',  
        'PORT': '3306', 
    }

  }

INSTALL_APPS
settings.py 파일을 보면 django 설치시에 기본적으로 설치된 프로그램이 표시된다.
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',

    'django.contrib.staticfiles',
)

django에 기본적으로 설치된 application을 사용하기 위해 데이터베이스 테이블을 사용할수 있게끔 설정이 필요

$ python manage.py syncdb
  Creating tables ...
  Creating table auth_permission
  Creating table auth_group_permissions
  Creating table auth_group
  Creating table auth_user_user_permissions
  Creating table auth_user_groups
  Creating table auth_user
  Creating table django_content_type
  Creating table django_session
  Creating table django_site

데이터베이스 테이블 생성이 완료되면 관리자 아이디/패스워드 설정을 한다.

  You just installed Django's auth system, which means you don't have any superusers defined.
  Would you like to create one now? (yes/no): yes
  Username (leave blank to use 'root'): 
  E-mail address: 
  Password:
  Password (again):
  Superuser created successfully.


3. mod_wsgi를 이용한 apache 연동

가. 웹서비스를 위한 아파치와의 연동을 위해 아파치의 apxs를 이용하여 mod_wsgi를 설치

# cd /usr/local/src/mod_wsgi-3.3
# ./configure --with-apxs=/usr/local/httpd/bin/apxs --with-python=/usr/local/python2.7/bin/python

# make && make install


나. 프로젝트 디렉토리에 django.wsgi 파일 생성

import os
import sys

sys.path.append('/home/newsb/public_html/sb_community')
sys.path.append('/home/newsb/public_html')

#os.environ['PYTHON_EGG_CACHE'] = '/home/newsb/public_html/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'sb_community.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()


다. apache httpd.conf 모듈 추가와 프로젝트에 작성된 wsgi path 추가

# vi /usr/local/httpd/conf/httpd.conf

LoadModule wsgi_module modules/mod_wsgi.so


<VirtualHost *:80>
    ------------
    WSGIScriptAlias / /home/newsb/public_html/sb_community/django.wsgi
    <Directory /home/newsb/public_html/sb_community>
      Order allow,deny
      Allow from all

    </Directory>

</VirtualHost>          

$ /etc/rc.d/init.d/httpd restart

웹 접속확인.


4. django application 생성

 $ python manage.py startapp sample_app

그 결과 다음과 같은 구조로 파일들이 생성된다.

samplepro/
      manage.py
      samplepro/
            __init__.py
            settings.py
            urls.py
            wsgi.py
      sample_app/
            __init__.py
            models.py
            tests.py

            views.py


settings.py파일에 새로 생성한 application을 추가한다.

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'sample_app',

)

application의 model 설정

application의 models.py 파일을 열어 django의 ORM(Object Relational Mapping) 기능을 이용하여 실제 데이터베이스 테이블과 맵핑하기 위한 모델을 작성한다

$ vi models.py

from django.db import models

class CommBoard(models.Model):
      subject = models.CharField(max_length=50, blank=True)
      name = models.CharField(max_length=50, blank=True)
      memo = models.CharField(max_length=200, blank=True)
      hits = models.IntegerField(null=True, blank=True)

      created_date = models.DateField(null=True, blank=True)

마이그레이션 작업내용을 만든다.
$ python manage.py makemigrations 
Migrations for 'sample_app':
  0001_initial.py:
    - Create model CommBoard

마이그레이션 작업내용을 실행시켜 mysql 테이블을 생성시킨다.
$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, contenttypes, board, auth, sessions
Running migrations:
  Applying board.0001_initial... OK



2014년 11월 21일 금요일

Raspberry Pi + MQTT 설치와 실행 테스트


$ cd /home/pi 
$ sudo mkdir Mosquitto 

//의존성 라이브러리 설치
$ sudo apt-get -y install libwrap0-dev libssl-dev

//mosquitto 다운로드
$ sudo wget http://mosquitto.org/files/source/mosquitto-1.0.2.tar.gz
$ sudo tar zxf mosquitto-1.0.2.tar.gz
$ cd mosquitto-1.0.2
$ make
$ sudo make install
$ sudo ldconfig
$ sudo make clean

// iptables의 1883 포트 허용
$ sudo iptables -A INPUT -p tcp -m tcp --dport 1883 -j ACCEPT
$ mosquitto

//환경파일
/etc/mosquitto/mosquitto.conf

//신규 터미널 생성 후 raspberry pi의 ssh 접속과 mosquitto subcribe
$ mosquitto_sub -d -t hello/world
Client mosqsub/6977-raspberryp sending CONNECT
Client mosqsub/6977-raspberryp received CONNACK
Client mosqsub/6977-raspberryp sending SUBSCRIBE (Mid: 1, Topic: hello/world, QoS: 0)
Client mosqsub/6977-raspberryp received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/6977-raspberryp sending PINGREQ
Client mosqsub/6977-raspberryp received PINGRESP

//신규 터미널 생성 후 raspberry pi의 ssh 접속과 mosquitto publish
$mosquitto_pub -d -t hello/world -m "안녕 안녕" 
Client mosqpub/7002-raspberryp sending CONNECT
Client mosqpub/7002-raspberryp received CONNACK
Client mosqpub/7002-raspberryp sending PUBLISH (d0, q0, r0, m1, 'hello/world', ... (12 bytes))

Client mosqpub/7002-raspberryp sending DISCONNECT

//subscribe했던 클라이언트에서 패킷 수신 확인 
Client mosqsub/6977-raspberryp received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (12 bytes))

안녕안녕