Tuesday, January 17, 2012

Android TCP and UDP basic code examples

This is a tiny app that has examples for both android TCP and UDP communications.

Remember, you should:

TCP - start the server first, because ServerSocket.accept() blocks

UDP - start the client first, because DatagramSocket.receive() blocks 

Friday, December 16, 2011

tofigureout: Django apps prefix

If all of all my Django apps are in the "apps" dir, is there something I can set so that I can directly write the app name instead of "apps.app_name" everytime.

It seems like I can do this only with sys.path or PYTHONPATH, which applies to all the django projects in the environment. This doesn't seem like a satisfactory solution. ... my summer project MozSecWorld didn't have this problem, I wonder what magic the setup for that did.

Setting up new Django project (with virtualenv(wrapper))

Download Django for Ubuntu in root:

1. cd /usr/local/lib, mkdir django-1.3.1 and cd into it

2a. wget http://www.djangoproject.com/download/1.3.1/tarball/ -O Django-1.3.1.tar.gz, got this step from here

2b. cd Django-1.3.1.tar.gz

2c. sudo python setup.py install

So my Django path is:
/usr/local/lib/django-1.3.1/Django-1.3.1

In user:
1. mkvirtualenv --no-site-packages vocab this is virtualenvwrapper setting up a new environment. Setup is logged in this old post.

$ mkvirtualenv --no-site-packages vocab
New python executable in vocab/bin/python
Installing setuptools............done.
Installing pip...............done.
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/preactivate
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/postactivate
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/get_env_details
mkdir: cannot create directory `/home/my_username/Projects/vocab': No such file or directory
-bash: cd: /home/my_username/Projects/vocab: No such file or directory
Searching for readline
Reading http://pypi.python.org/simple/readline/
Reading http://www.python.org/
Reading http://github.com/ludwigschwardt/python-readline
Best match: readline 6.2.1
Downloading http://pypi.python.org/packages/source/r/readline/readline-6.2.1.tar.gz#md5=9604527863378512247fcaf938100797
Processing readline-6.2.1.tar.gz
Running readline-6.2.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-9h67Mv/readline-6.2.1/egg-dist-tmp-nNoSrj
/usr/bin/ld: cannot find -lncurses
collect2: ld returned 1 exit status
error: Setup script exited with error: command 'gcc' failed with exit status 1
Downloading/unpacking Jinja2==2.3.1
  Downloading Jinja2-2.3.1.tar.gz (428Kb): 428Kb downloaded
  Running setup.py egg_info for package Jinja2
    
    warning: no previously-included files matching '*' found under directory 'docs/_build/doctrees'
Downloading/unpacking MySQL-python==1.2.3c1
  Downloading MySQL-python-1.2.3c1.tar.gz (89Kb): 89Kb downloaded
  Running setup.py egg_info for package MySQL-python
    
Downloading/unpacking lxml==2.2.6
  Downloading lxml-2.2.6.tar.gz (2.9Mb): 2.9Mb downloaded
  Running setup.py egg_info for package lxml
    Building lxml version 2.2.6.
    NOTE: Trying to build without Cython, pre-generated 'src/lxml/lxml.etree.c' needs to be available.
    ERROR: /bin/sh: xslt-config: not found
    
    ** make sure the development packages of libxml2 and libxslt are installed **
    
    Using build configuration of libxslt
    
    warning: no previously-included files found matching '*.py'
Downloading/unpacking PIL
  Downloading PIL-1.1.7.tar.gz (506Kb): 506Kb downloaded
  Running setup.py egg_info for package PIL
    WARNING: '' not a valid package name; please use only.-separated package names in setup.py
    
Obtaining check from git+git://github.com/jbalogh/check.git#egg=check
  Cloning git://github.com/jbalogh/check.git to /home/my_username/.virtualenvs/vocab/src/check
  Running setup.py egg_info for package check
    
Downloading/unpacking pyflakes (from check)
  Downloading pyflakes-0.5.0.tar.gz
  Running setup.py egg_info for package pyflakes
    
Downloading/unpacking pep8 (from check)
  Downloading pep8-0.6.1.tar.gz
  Running setup.py egg_info for package pep8
    
Downloading/unpacking path.py (from check)
  Error  while getting http://pypi.python.org/packages/source/p/path.py/path.py-2.2.2.tar.gz#md5=3a3262b9049d294ddbd6f7453d45e699 (from http://pypi.python.org/simple/path.py/)
Exception:
Traceback (most recent call last):
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/basecommand.py", line 126, in main
    self.run(options, args)
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/commands/install.py", line 223, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/req.py", line 961, in prepare_files
    self.unpack_url(url, location, self.is_download)
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/req.py", line 1079, in unpack_url
    return unpack_http_url(link, location, self.download_cache, only_download)
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/download.py", line 429, in unpack_http_url
    resp = _get_response_from_url(target_url, link)
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/download.py", line 465, in _get_response_from_url
    resp = urlopen(target_url)
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/download.py", line 84, in __call__
    response = urllib2.urlopen(self.get_request(url))
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1185, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1160, in do_open
    raise URLError(err)
URLError: 

Storing complete log in /home/my_username/.pip/pip.log

2. Since I installed with no-site-packages, I need to add Django to the virtualenv
add2virtualenv /usr/local/lib/django-1.3.1/Django-1.3.1

3. follow the Django tutorial

3b. In python manage.py sql app_name I got this error:
...
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
so I did
pip install MySQL-python

3c. I installed my app inside an "apps" folder, so here is what I did:
mkdir apps; touch apps/__init__.py
python manage.py startapp app_name
mv app_name apps
vim settings.py and add into INSTALLED_APPS 'apps.apps_name'
python manage.py sql app_name
Note that you should have 'apps.app_name' in INSTALLED_APPS or else it's not going to find it and give you this error instead:
Error: No module named app_name

Friday, November 11, 2011

Linux show total lines of files in a directory (sorted by line numbers)

Linux showing total number of lines of each file in a directory:

wc -l *

Even better, you can have the results sorted by line numbers:

wc -l * | sort -n -k1

-n         compare according to string numerical value
-k#       define start position

Friday, October 21, 2011

Deploying Django

I can either do it the right way or the cheating way in screen, that might crash Django when certain errors are encountered. The screnning way might need this to keep the process running when I logout


TODO, suggested by mark:
with apache in front, mod_wsgi handles the processes for you
.. and standard server config takes care of the apache stuff

----

SSL .. Django admin through port forwarding?

Setting up MSW on Rackspace

./manage.py shell

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

In root install: pip install mysql-python.

If you I got another error ...
In file included from _mysql.c:29:0:
pymemcompat.h:10:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1
... then you need to install sudo apt-get install python-dev

(I searched for solutions for a long time, trying to find my "/usr/local/mysql" on ubuntu, but I don't have it (even though I have mysql).)


Now ./manage.py shell has
ImportError: No module named OpenSSL
From root, pip install pyopenssl


Now ./manage.py shell has
ImportError: No module named bycrypt
From root, pip install py-bcrypt

AND THEN I GOT MANAGE.PY SHELL!!!
import django! works on it too!!!!!!!!
$ ./manage.py shell
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import django

Whoa I can even run it on my rackspace IP! :D Even though it crashes, at least it can get the request!

$ ./manage.py runserver 50.57.147.82:8000
Validating models...

0 errors found
Django version 1.3.1, using settings 'MozSecWorld.settings_local'
Development server is running at http://50.57.147.82:8000/
Quit the server with CONTROL-C.
[21/Oct/2011 03:23:36] "GET / HTTP/1.1" 301 0

Traceback (most recent call last):
...
TemplateSyntaxError: Caught ImportError while rendering: No module named jinja2
[21/Oct/2011 03:23:37] "GET /msw/ HTTP/1.1" 500 2909
So I did, from root, pip install jinja2

AND MOZSECWORLD IS UP at http://50.57.147.82:8000/!!!!!!!! :D:D:D