2018年4月29日 星期日

How to set up cgi module for apache2

At this moment, i am studying CGI(common gateway interface) script with python. Need to setup a temporary local website for test. Some notes here.

Environment
1. ubuntu 14 server
2. apache2 install

I am not sure any python copy should be installed or not necessary.

First need to edit /etc/apache2/conf-enabled/serve-cgi-bin.conf

<IfModule mod_alias.c>
 <IfModule mod_cgi.c>
  Define ENABLE_USR_LIB_CGI_BIN
 </IfModule>
 
 <IfModule mod_cgid.c>
  Define ENABLE_USR_LIB_CGI_BIN
 </IfModule>
 
 <IfDefine ENABLE_USR_LIB_CGI_BIN>
  ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
  <Directory "/usr/lib/cgi-bin">
   AllowOverride None
   Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
   Require all granted
  </Directory>
 </IfDefine>
</IfModule>
 
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
substitute the directory after "ScriptAlias" with the directory you want to put your cgi 
script files. 
Note. this directory must fall in access range of apache2. You can edit apache2.conf under 
/etc/apache2/ directory to add your directory 
<Directory "/var/www/cgi-bin">
   AllowOverride None
   Options ExecCGI
   Order allow,deny
   Allow from all
</Directory>

<Directory "/var/www/cgi-bin">
   Options All
</Directory>
cgi module is not enabled by default while apache2 installed. You have to manually add it by make a link to /etc/apache2/mods-enabled directory with below command

$ cd /etc/apache2/mods-enabled
$ sudo ln -s ../mods-available/cgi.load
then reload apache2

$ sudo service apache2 reload
It's all set. You can now put your first cgi script to your cgi-bin directory and test it with your favorite browser under http://localhost/cgi-bin/xxx.py
oh! one more thing! Remember to change access mode to your script file.
sudo chmod +x /usr/lib/cgi-bin/xxx.py

2018年4月3日 星期二

Python install Modules

Besides standard Library, there are lots of modules developed by independent contributors and academy or science group. You can use commands to download and install these modules with built in pip install program.

python -m pip install --user modulename 

Here list some modules of categories

1. web framework

  • Django
  • Pyramid
  • Web2py
  • flask
2. Graphic processing
  • PIL
  • Pillow
3. Science and Math
  • numpy
  • Matplotlib
  • pandas
  • scikit-learn
4. command line operation
  • fabric
  • paramiko
5. Nature language processing
  • nltk
  • textblob
  • jieba
6. network client
  • requests
  • pycurl
7. database protocol
  • mysql-python
  • pymongo
  • psycopg2

Python Study Note (1)

Besides ordinary Python Manual reading, i would like to put some notes here to refresh my bad memory. There are many key words that i need to keep in mind when learning new computer language especially this language being a huge different compared to what i used for living(c, assembly).

Well skip the interpreter part which i see tiny chance of using it.

Typical python module definition file which have file extension of *.py have below format.

if you have multiple version of python install in your system, you can specify which version you want your program to run with.
#below  check your py.ini for defaults of "python" version

#! python  

#below specify the encoding of this python source file. utf-8 is default.

#-*-coding: utf-8-*-  


#then import some module you reference

import numpy

import sys,os


def fun1(a):

    pass


def main():

    pass


#below will use module file as script to run main, mainly for test purpose 

if __name__ == '__main__':

    main()  
There are some coding style that it would be nice to follow.
  • Use 4-space indentation, and no tabs.
    4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Tabs introduce confusion, and are best left out.
  • Wrap lines so that they don’t exceed 79 characters.
    This helps users with small displays and makes it possible to have several code files side-by-side on larger displays.
  • Use blank lines to separate functions and classes, and larger blocks of code inside functions.
  • When possible, put comments on a line of their own.
  • Use docstrings.
  • Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4).
  • Name your classes and functions consistently; the convention is to use CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument.
  • Don’t use fancy encodings if your code is meant to be used in international environments. Python’s default, UTF-8, or even plain ASCII work best in any case.
  • Likewise, don’t use non-ASCII characters in identifiers if there is only the slightest chance people speaking a different language will read or maintain the code.

2018年3月7日 星期三

AI study note

AI is getting more and more real life applications recent couple years. By realize this, i think it is time to know more detail of it. There's more resources on the web then before.
The most famous online course in this field is opened by Andres Ng on coursera. He is the icon of current AI industry.
Andrew Ng Stanford University

There's also a simple introduction to AI learning by Shival Grupta
Shiva Gupta blog

Albeit there's no pre-request of programming language, but i decide to cut in follow Shival's path with Python and google's tensorflow.


2017年8月26日 星期六

linux learn note

install package
download package either by browser or wget a .deb file
sudo dpkg -i XXXXX.deb
if meet issues, try below
sudo apt-get -f install
sudo dpkg -i XXXXX.deb

to update current install package
sudo apt-get update

install svn
1. install apache2
sudo apt-get install apache2
configure apache2
....

2. install svn
sudo apt-get install subversion
configure svn and start svn daemon


To list packages install in system
pkg-config --list-all
To uninstall a make install
sudo make uninstall

if not work 
try 
checkinstall
or use 
make -n install
to trace install steps and uninstall manually

2017年8月22日 星期二

EFI switch Graphic Mode

UEFI switch between graphic/Text mode example. 

  EFI_CONSOLE_CONTROL_PROTOCOL  *ConsoleControl = NULL;

  Status = gBS->LocateProtocol (&gEfiConsoleControlProtocolGuid, NULL, (VOID**)&ConsoleControl);

  if (ConsoleControl != NULL)
  {
    ConsoleControl->SetMode(ConsoleControl, EfiConsoleControlScreenGraphics);
  }

Only below two modes can choose from.
  EfiConsoleControlScreenText,
  EfiConsoleControlScreenGraphics


Learn one thing. Even switch to graphic mode, you can still use EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL or even Print.

2015年3月31日 星期二

Overcome the Eight Barriers to Confidence

轉載文 建立自信心非常有用
To get a more confident You in the new year — or a more confident company, community, family, or team — first know what gets in the way. The best resolutions will go nowhere without the confidence to stick with them.
Confidence is an expectation of a positive outcome. It is not a personality trait; it is an assessment of a situation that sparks motivation. If you have confidence, you’re motivated to put in the effort, to invest the time and resources, and to persist in reaching the goal. It’s not confidence itself that produces success; it’s the investment and the effort. Without enough confidence, it’s too easy to give up prematurely or not get started at all. Hopelessness and despair prevent positive action.
To muster the confidence to work toward your goals, avoid these eight traps:
  • Self-defeating assumptions. 
    • You think you can’t, so you don’t.  A British Olympic runner is so rattled by a misstep that cost her a contest that she dropped out of the next. A company team decides that a popular world leader is so far out of their league that they don’t issue an invitation to speak at their customer event. Talented women sometimes “leave before they leave,” as Sheryl Sandberg puts it, assuming that they won’t be promoted (or succeed when they have children) so they start behaving like they’re departing years before departure, thus foreclosing their options. It’s one thing to be realistic, it’s another to behave like a loser before entering the game.
  • Goals that are too big or too distant. 
    • I know how often leaders say they want to tackle BHAGs — “big hairy audacious goals.” But having only enormous goals can actually undermine confidence. The gap between a giant goal and today’s reality can be depressing and demotivating. Confidence comes from small wins that occur repeatedly, with each small step moving you closer to the big goal. But the small steps must be valued and turned into goals themselves. Winners think small as well as big.
  • Declaring victory too soon. 
    • This is the dieter’s dilemma: lose the first few pounds, and feel so good that you reward yourself with chocolate cake, and when the pounds go back on, you feel so discouraged that you have more cake to feel better. I saw this pattern in a college football team that was coming off a 9-year losing streak (yes, 9 years!). After winning the first game in nearly a decade, a team member shouted that now we’ll win the championship. First, of course, they had to win the next game — which they didn’t. Step-by-step discipline builds confidence.
  • Do-it-yourself-ing.  
    • It’s a trap to think you can go it alone, without a support system and without supporting others. Losing teams have stars, but they focus on their own records, not how well the whole team does; the resulting resentments and inequalities provoke internal battles that drag everyone down. To build your confidence, think about building the confidence of others and creating a culture in which everyone is more likely to succeed, whether through mentoring them or recognizing their strengths. Giving to others boosts happiness and self-esteem, as numerous research studies show. Supporting them makes it easier to ensure that they support you.
  • Blaming someone else. 
    • Confidence rests on taking responsibility for one’s own behavior. Even in difficult circumstances, we have choices about how to respond to adversity. Whining about past harms reduces confidence about future possibilities. When the blame game is carried out within companies, everyone loses confidence, including external stakeholders. Confidence is the art of moving on.
  • Defensiveness. 
    • It’s one thing to listen and respond to critics; it’s another to answer them before they’ve done anything. Don’t defend yourself if you’re not being attacked. Apologize for your mistakes, but don’t apologize for who or what you are. Instead, take pride in where you’ve come from and lead with your strengths.
  • Neglecting to anticipate setbacks. 
    • Confidence involves a dose of reality. It is not blind optimism, thinking that everything will be fine no matter what. Confidence stems from knowing that there will be mistakes, problems, and small losses en route to big wins. After all, even winning sports teams are often behind at some point in the game. Confidence grows when you look at what can go wrong, think through alternatives, and feel you are prepared for whatever might happen.
  • Over-confidence. 
    • Confidence is a sweet spot between despair and arrogance. Don’t let confidence slip over into the arrogant end. Over-confidence is the bane of economies (e.g., the irrational exuberance that preceded the global financial crisis), corrupt leaders (who assume they’re so necessary that they won’t get into trouble for a small expense account fudge), or individuals who swagger and feel entitled to success rather than working for it. Arrogance and complacency lead to neglect of the basics, deaf ears to critics, and blindness to the forces of change — a trap for companies as well as individuals. Sure enough, like the old proverb that “pride goeth before a fall,” the slide into a losing streak often begins with a winning streak. A little humility goes a long way to moderate arrogance and keep just the right amount of confidence.

Remember, it’s not enough just to feel confident. You have to do the work. But with an expectation of success, you can try new things, form new partnerships, contribute to shared success, and revel in small wins that move you toward bigger goals.