Friday, April 19, 2013

Bug: Android google-tv-remote library

If you use the android google-tv-remote library, and your device modal contains some special characters. it might crashed your android application .

When you check the code, it generate the x509 name through the following logic,

image

So if you are run a new ATT HTC ONE X+ phone, you might get the Name like

CN=anymote/evitareul/evitareul/HTC One X+/devid

however, according to the naming conventions, the ``special'' characters required by RFC2253 in a field That is ,+"<>; so if you don’t replace it or encoding it, you will get Exception. If you don’t catch it, it might crash your application.

image

the code only catch the generalsecurityeception, not the naming exception.

so you need to encode or replace the special characters. and catch the exception just in case.

Monday, April 15, 2013

How to : run python script as a windows service on windows 2008

Just setup one django python application, always we run the script Python manage.py start 0.0.0.0:8080 to start a web application, if you want to run this a service. here is a quick tutorial,

Download the windows resource kit which contains one utility called svcany. basically you can run cmd as a service. most command can run on windows server 2008 all though they are designed for windows server 2003. then install it.

Here is a kb about the svcany. http://www.microsoft.com/en-us/download/details.aspx?id=17657

then use the built in command SC to create one windows service. we just point the binPath to the scany.exe location.

SC Create Askbot80 binPath= “d:\windows resource kit\tools\svcany.exe”
#remember there must be a space between binpath= and the value.


Now when you go to services mmc, you can see the service is there.
image

then run regedit, to put the python script into the parameters settings.

go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\, you can see the service we just created is there already.

we just create one new key called Parameters, and under Parameters create one string key named Application , then enter the given python and python script there.
image

then you can start the service we just created.

Wednesday, April 3, 2013

How to : Test Microsoft Enterprise Library 5.0 tutorial , Basic Logging support.

Microsoft enterprise library has been updated with the support for .net framework 4.0 , supporting for the WinRT is coming, you can check the uservoice to vote for the features you want. Here is the quick tutoria to get started , we run some basic click and point setup, then do the GUI based config. then same basic coding,

Installation,

you can setup any project and using the NuGet to search enterprise library, then all the available blocks and extensions are there.

Search “Enterprise” in the nuget source, you will see a lot blocks , we pickup the logging block first,
image

it will show you all the dependencies , here the unity is the IOC Container , common is the shard base library .
image

Now our project has the required assembilies, Next step, we need one editor to do the configuration. Download and Install this Config Editor. and restart the IDE, then you can right click app.config, and see the editor option with along with the WCF configuration
image

Now let’s do some basic configuration to turn on the logging. basically we inject some implementation of the logwriter and it’s dependencies for implementations.

click the blocks menu, you will see all the available blocks we can configure. (once we add more nuget package like cryptography , you will see more options in the menu)
image

by default,  it pick up one eventlog listener and using the giving format. . we just leave it as it. then save the config
image

Now, let’s write a helloword app to write some dummy log.

get a container first, then we can retrieve the logwriter from the contonainer, I will use the unitycontainer.

var container = new UnityContainer();
container.AddNewExtension<EnterpriseLibraryCoreExtension>();


then let’s get the reference to logwriter. then dump some message,
image

Run it, we can see the log in the event log there.
image

Now app is there, we can simply change the config file, and change the log to a text file or event a Database table.
right click the target listener and add, you can see we have several ones. we just pick up the flat file.
image

image

and chose the formmater to the default one, which you can config it too, like add more fileds.

then assign this listener to the right category. for this demo just assign to the general one.
image


Save it and run. we can see the log file called trace.log was generated. with the following contents
image

if we want to save it to DB, search NUget, and add the logging for db blocks
image

then we you add log listener, you will see a new one.

image

for the database listener to work, need run the scripts under the package folder and specify the db connection string. that’s it.

Thursday, February 14, 2013

Python REST Framework FLASK

Flask is a very lightweight REST framework for python, here is a very basic tutorial to get started.

install Flask,
pip install flask,
image
then create one module to test the flask.

hello world test,
image
then run the web server,
image
now http://localhost:8000/, you will see the hellofalsk page,
image

test to list folders and return as Json,
image
here we put prefix in the url template. in the method we can assess the url passed from user side.
image

test to get the request information, like the user-agent. just import the global request.
image

image

Silverlight load 98% on MAC and stuck there.

you may get some 98% loading and never ends,

image

basically this means the Silverlight plugin is crashed anyhow. so just remove the plugin and instll it again, to do that,

Open the Finder:
HD>LIbrary>Internet Plugins folder: move to the trash Silverlight.plugin
HD>Library>Applications Support>Microsoft: move the Silverlight folder to the trash.
Also, check in Your User Account>Library>(same end locations as above).
then restart Safari.

Testing Python for C# programmers

'''
Created on Feb 13, 2013

@author: Android.Y.You
'''
 

def main():
    print "you are in main"


#* operator of String
print '*'*20

 
import random

#format output
print "here is one random number %i " % ( random.randint(5,10))

#using type to get type information
print type(20)
print type(main)


#import everything
#before

import math
print math.pi
#after
from math import *
print pi

#comment,doc
def SayHello(msg):
    """SayeHello Function
    enter the msg as the input, typically a string
    """
    print "Hello " + msg

SayHello("ss")

#PrimeNumbers, else with for means no match.
def prime(n=100):
    for i in range(2,n):
        for j in range(2,i):
            if (i % j==0):
                break
        else:
            print i
           
prime(10)


#comprehensive list
s=range(1,20)
t=[x for x in s if x % 3==0 and x!=3 ]
print t

#lambda expression
def addx(n):
    return lambda x:x+n
add3=addx(3) # add 3
print add3(10) # 13

#keyboard input
#get input,using int as the converter, raw_input to get console in
id=int( raw_input("enter a number"))
print id>10

#list

list=range(1,3)
print type(list)
for l in list:
    print l
   
#list a mutable
list[0]=100
print list

#tuples, tuples are immutable
tu=(1,2,3)
print tu

#dictionary

d={"one":"1","two":"2"}
for k,v in d.items():
    print k,v
   
   
#fiel write
fout=open("out.txt","w") #a+ for append
fout.writelines("helloworld")
fout.close()

#write a object
class Person(object):
    Name="yourname"
    Age=20
    def __init__(self,name,age):
        self.Name=name
        self.Age=age
    def __str__(self, *args, **kwargs): #overwrite the java like tostirng
        return "name:%s,Age:%i" % (self.Name,self.Age)
p=Person("android",25)
print p

import pickle
print pickle.dumps(p) #object to strings

#file path
import os
print os.getcwd()

#walk for files
def walk(dirname):
    for file in os.listdir(dirname):
        s= os.path.join(dirname,file)
        if(os.path.isfile(s)):
            print s
        else:
            walk(s)
   
walk(r"C:/temp/")


#gui, tkinter.
from Tkinter import *
root=Tk()
w=Label(root,text="Helloworld")
w.pack()
root.mainloop()

 

#AOP, using the decroator
def logaop(func):
    print "binded"
    def ins(*a,**b):
        print "---before call the method"
        func(*a,**b)
        print "---after call the method"
     
    return ins

@logaop
def GetInfomrationFromDB(informationkey):
    print "You get the result for " + informationkey
  

 

GetInfomrationFromDB("test")


output

********************
here is one random number 9
<type 'int'>
<type 'function'>
3.14159265359
3.14159265359
Hello ss
2
3
5
7
[6, 9, 12, 15, 18]
13
enter a number10
False
<type 'list'>
1
2
[100, 2]
(1, 2, 3)
two 2
one 1
name:android,Age:25
ccopy_reg
_reconstructor
p0
(c__main__
Person
p1
c__builtin__
object
p2
Ntp3
Rp4
(dp5
S'Age'
p6
I25
sS'Name'
p7
S'android'
p8
sb.
C:\Project\python\wp\FirstHello\src
C:/temp/a\New Text Document.txt
C:/temp/test.html

Thursday, February 7, 2013

Test Twisted Python on windows

Twisted is one popular event-driven network engine writen in python. basically, it can create a web server as the node.js do, and also provide the similar features as socket io like the pub/sub through internet. to test it, we need install python, and load the necessary dependencies. and PIP is one tool to do the package installation for you. here are some basic steps to test twisted.

Install python 2.7, just download the installer the run it, then put the path of the installed directory to system path environment. I just installed it under c:\python27. then we can run python to launch the interactive shell.
image

then install the pip utility.
PIP require setuptools utility, http://pypi.python.org/pypi/setuptools
you can pickup the exe download, or just download the 64 bit setup script. then download the pip package. and extract it to a local folder.
image
when done, it will create some utility under the python27 scripts folder, like pip.exe. then we can add this folder the system path env as well.
image

Install twisted, for the twisted , it require zope.interface. now we have pip, just run pip install zope.interface to install the dependency.

image

Now download the twisted windows installer http://twistedmatrix.com/trac/, click and good to go.
after all done, we can open the python shell, and list the twisted package.

then create one simple web server,and run it,
image

image

 
Locations of visitors to this page