almost 2 years ago

The common command line arguments that I use with the netstat command:

-t: display tcp connections
-u: display udp connections
-l: display ports that are in LISTEN state
-p: display process id
-n: display ip addresses instead of host names

To see if a port has been occupied:

> netstat -tulpn | grep :port_number
 
almost 2 years ago

I was trying to start a Postfix mail server on a Ubuntu 12.4:

> service postfix start
* Starting Postfix Mail Transport Agent postfix                                     [ OK ]

> telnet localhost 25
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

What? I just started the service and it should be running on port 25 but telnet won't connect, so I had to dig more:

> netstat -tulpn | grep :25 | LISTEN

Nothing?! After some Google search, I located the log file at /var/log/mail.err:

> less /var/log/mail.err

fatal: open lock file /var/lib/postfix/master.lock: cannot open file: Permission denied

No Permission? Ok, let's give it the permission:

> chown postfix.postfix -R /var/lib/postfix
> service postfix start
 * Starting Postfix Mail Transport Agent postfix
   ...done.

> netstat -tulpn | grep :25 | grep LISTEN
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      22166/master

Great, it worked!

P.S. chown postfix.postfix = chown postfix:postfix = chown owner_name:group_name

 
almost 2 years ago

I have a site which sends out emails through my corporate email account (Outlook), here are the steps need to send emails using Django:

Let's say my username is cheng@blah.com and the password is 123 for my email account.

  1. Make sure the following settings are in your settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'email.blah.com' # double check the settings in your outlook mailbox to make sure the host name is correct

EMAIL_PORT = 587  # double check the settings in your outlook mailbox and make sure the port number is correct

EMAIL_HOST_USER = 'cheng'  # don't include the @blah.com part! I have made this stupid mistakes before

EMAIL_HOST_PASSWORD = '123'
  1. In your code:
from django.core.mail import EmailMessage
import traceback

def send_email():
    email = EmailMessage(
        subject='subject',
        body='message',
        from_email='cheng@blah.com',
        to=['someone1@blah.com', 'someone2@blah.com'],
        cc=['someone3@blah.com'],
        reply_to=['cheng@blah.com'],  # when the reply or reply all button is clicked, this is the reply to address, normally you don't have to set this if you want the receivers to reply to the from_email address

    )
    email.content_subtype = 'html' # if the email body contains html tags, set this. Otherwise, omit it

     try:
        email.send(fail_silently=False)
        return HttpResponseRedirect(reverse('apply:success'))
    except Exception:
        print traceback.format_exc() # you probably want to add a log here instead of console printout

That should be it :)

 
almost 2 years ago

Two special characters are involved here:

  • Carriage Return (CR or \r in many programming languages)
  • Line Feed (LR or \n)

I never understood what those terms mean until I read that they are inherited from typewriters. When typing on a type writer, two physical actions have to happen in order to continue typing on the next line. First, the carriage needs to return to the very beginning (the far left and makes the ding sound). Then, the typewriter needs to move the page up a little so that we can type on a new line. The moving page up action is called line feed.

Windows uses CR+LR (i.e. \r\n) to denote a new line but on Unix and Unix-like machines (e.g. Linux), only LF or \n is used.

Some interesting behaviour of how CRLF is treated in JavaScript [*]:

JavaScript and XPath treat CRLF pairs as two line breaks. ^ matches in the middle of and after CRLF, while $ matches before and in the middle of CRLF.

 
almost 2 years ago

The functionality of the for loop and the forEach method of the array object are very similiar: looping through a list of items and do something with it. The differences lies below:

Break & Continue

You cannot break inside forEach:

var arr = [1,2,3,4,5];
arr.forEach(function(element, index){
    if (element === 3){
     break; // SyntaxError: Illegal break statement

  }
});

But you can simulate continue:

var arr = [1,2,3,4,5];
arr.forEach(function(element, index){
    if (element === 3){
     continue; // SyntaxError: Illegal continue statement

  }
  console.log(element);
});

arr.forEach(function(element, index){
    if (element === 3){
     return; // return will not break out of the forEach loop, it works like continue in this case

  }
  console.log(element);
});

// output:

1
2
4
5

Variable Scope

Another are where for loop and forEach differs, is the variable scope:

for(var i=0;i<5;i++){
}
console.log(i); // output: 5

Everything defined in a for loop belongs to its containing environment. But since forEach uses a function, you don't have to worry about variables leaking out of scope:

var arr = [1,2,3,4,5];
arr.forEach(function(element, index){
});
console.log(element); // ReferenceError: element is not defined

Performance

As being pointed out by this stackoverflow answer, for loop is faster than forEach. If you need to loop through tens of thousands of records, use the for loop (actually, generators maybe better for this scenario). Otherwise, forEach is a better choice as humans reading your code can easily understand your intension.

 
almost 2 years ago

I run into a problem today, here is the snippet:

var i = 101;
console.log('101: ' +  i.toString(2));
console.log('101 >> 1: ' + (i >> 1).toString(2));

var l = -101;
console.log('-101: ' + l.toString(2));
console.log('-101 >> 1: ' + (l >> 1).toString(2));'

Output:

"101: 1100101"
"101 >> 1: 110010"
"-101: -1100101"
"-101 >> 1: -110011"

Why -101 >> 1 is -110011 instead of -110010?

Here is what I learned by reading Professional JavaScript for Web Developers:

When js stores a negative number, it does the following things:

  1. get the binary representation of the absolute value of the negative number
  2. replace 0s with 1s and 1s with 0s
  3. add 1 to the result of step 2

So in my case -101 >> 1, we first convert -101 to its binary representation:

The binary representation of Math.abs(-101) is:

0000 0000 0000 0000 0000 0000 0110 0101

invert the 0s and 1s:

1111 1111 1111 1111 1111 1111 1001 1010

add 1 to the end:

1111 1111 1111 1111 1111 1111 1001 1011

Now, shift it to the right by 1:

1111 1111 1111 1111 1111 1111 1100 1101

The binary above should be the correct result of -101 >> 1, but when logging a negative number's binary representation, Javascript simply puts a negative sign in front of the binary representation of the positive number:

var x = 15;
console.log(x.toString(2)); // output: 1111


var y = -15;
console.log(y.toString(2)); // output: -1111

For our example, this means that when logging the result of -101 >> 1, JS will output minus sign + the binary representation of the positive number. But the positive number is not 101 >> 1 because 101 >> 1 gives you:

(101 >> 1).toString(2);  // output: 110010

(-101 >> 1).toString(2); // output: -110011, not -110010!

To get the correct result, we have to reverse the aforementioned step 1-3:

1111 1111 1111 1111 1111 1111 1100 1101   // this is the result we get from step 4

Reverse step 3 by subtracting 1, we get:

1111 1111 1111 1111 1111 1111 1100 1100

Reverse step 2 by invert 0s and 1s:

0000 0000 0000 0000 0000 0000 0011 0011

Reverse step 1 by converting this binary to integer:

parseInt(110011, 2); // output: 51

Finally, when JS logs the result of -101 >> 1, it should be in the format of minus sign + the binary representation of 51:

(51).toString(2);        // output:  110011

(-101 >> 1).toString(2); // output: -110011
 
almost 2 years ago

I was running a django app on a linux machine and when I tried to login to the admin page, I kept getting redirected to the login page itself. It looks like this:

  1. POST /login/ 302
  2. GET / 302 < - should give me a 200 single I have logged in successfully but returns 302 instead
  3. GET ?next=/ 200 <- back to login page again

The site works when I use ./manage runserver but it wouldn't work when I use gunicorn to run it. I clear the cookie but it didn't work. I tried to set debug = False didn't work either. Eventially I found the problem, I wanted to use the database to store sessions, so I did this:

INSTALLED_APPS = (
    ...,
    django.contrib.sessions
)

SESSION_ENGIN = 'django.contrib.sessions.backends.cache'

For some reason, my session cache is messed up and I have to use this setting instead:

SESSION_ENGIN = 'django.contrib.sessions.backends.cached_db'

Instead of storing cache in memory which disappears after server restart, cached_db writes it to the database. I have used the cache option before and worked but not this time.

 
almost 2 years ago

I had the pleasure of setting up my own pypi server last week because the server I had to work with is behind a corporate firewall.

To setup your own pypi server, here is what to do:

  1. Download pypiserver, then:
unzip pypiserver-1.1.10
cd pypiserver-1.1.10
python setup.py install

Config pypyserver settings:

cd ~
touch .pypirc

Enter the following info in to .pypirc:

[distutils]
index-servers =
  pypi
  local

[pypi]
username:
password:

[local]
repository: http://localhost:8080
username:
password: 

Setup the packages dir, this is the place where you store the packages:

cd ~
mkdir packages

Run!

pypi-server -p 8080 ~/packages

You might want to keep the server running even after you logout of terminal:

nohup pypi-server -p 8080 ~/packages &

You need to stuff your packages folder so that there will be something for pip install, here are essential packages you need to at least get a virtual environment running:

  • pbr
  • pip
  • setuptools
  • six
  • stevedore
  • virtualenv
  • virtualenv-clone
  • virtualenvwrapper
  • wheel

To download these packages, just go to https://pypi.python.org/pypi/ and search for the package name, then download the tar.gz version of that package. Once downloaded, move the zip file to the ~/packages folder.

To verify that the server is up and running, simply go to localhost:8080/simple/, you should see a list of package names (or empty if you haven't copied any packages into the packages folder yet).

Config your client to download python packages from your server:

cd ~
mkdir .pip
touch .pip/pip.conf

Put the following lines into pip.conf

[global]
index-url = http://localhost:8080/simple
[install]
trusted-host = localhost

Done! Try using pip install and it will download a package instantly from your ~/packages folder and install it :)

 
about 2 years ago

Found an excellent snippet here by monikkinom, which compresses all uploaded images to jpg before saving to disk:

from PIL import Image as Img
import StringIO

class Images(models.Model):
    image = models.ImageField()

    def save(self, *args, **kwargs):
        if self.image:
            img = Img.open(StringIO.StringIO(self.image.read()))
            if img.mode != 'RGB':
                img = img.convert('RGB')
            img.thumbnail((self.image.width/1.5,self.image.height/1.5), Img.ANTIALIAS)
            output = StringIO.StringIO()
            img.save(output, format='JPEG', quality=70)
            output.seek(0)
            self.image= InMemoryUploadedFile(output,'ImageField', "%s.jpg" %self.image.name.split('.')[0], 'image/jpeg', output.len, None)
        super(Images, self).save(*args, **kwargs)

According to the official doc, the parameters for the InMemoryUploadedFile are:

file, field_name, name, content_type, size, charset, content_type_extra=None

Bonus

If you want to scale the image and still keeps its aspect ratio:

new_width = 800
img.thumbnail((new_wdith, new_width * self.image.height / self.image.width), Img.ANTIALIAS)
 
about 2 years ago

To understand the concept of iterator in Javascript, it is important to memorize the following three protocal:

1. Mark an object iterable

To mark an object iterable, you need to add a Symbol.iterator property to it:

var obj = {};
obj[Symbol.iterator] = function() { return iteratorObject; }

The Symbol.iterator part looks really odd if you don't know what Symbol is all about. One way to think about it is to see it as metadata that describes the hidden behaviors of an object. When an object is marked with the iterator symbol, it means you can loop through it like an array.

2. Return an iterator object

The code snippet above returns an iterator object. An iterator object needs to have a next method which is a function that returns an iterator result object:

iteratorObject = {
    next: function(){
        return iteratorResultObj;
    }
}

3. Return an iterator result object

Finally, I mean finally we can return some real values rather than wrappers that conforms to the iterable and iterator protocal.The iterator result object has to have two properties, one named value and the other named done.

iteratorResultObj = {
  value: ...,
  done: ...  // either true or false

}

As long as an object has the three traits explained above, it is iterable and can be used in a for...of loop or being spreaded [...myObj].

Putting it all together

var obj = {};

obj[Symbol.iterator] = function (){  // mark an object as iterable

  var i = 1;
  return {    // this is the iterator object

    next: function(){
      return i < 5 ? 
      {  // this is the iterator result object

        value: i++,
        done: false
      } : { done: true } // when the iterator reaches the end, value = undefined and done = true

    }
  }
}

var iter = obj[Symbol.iterator]();
console.log('value: ' + iter.next().value);
console.log('value: ' + iter.next().value);
console.log('value: ' + iter.next().value);
console.log('value: ' + iter.next().value);
console.log('done: ' + iter.next().done);

// the code above is equivalent of the following loop

for(var j of obj){
  console.log(j);
}

Here is a jsbin link of the code above.

Bonus

To use ES6 syntax and define an iterator within a class:

class MyArray {
    constructor(...items) {
        this.items = items;
    }

    [Symbol.iterator]() {
      let pos = 0;
      let data = this.items; // the value of `this` changes to undefined inside the next() function below, thus, we need to store it in a variable first

      return {
        next: () => {
            return pos < data.length? 
              { value: data[pos++], done: false } 
            : { done: true }
        }
      }
    }
}

let arr = new MyArray(1,2,3)

for(const i of arr){
  console.log(i);
}

jsbin link