1

I'm tired of typing manage.py startserver 10.211.55.4:4000, so decided to make an alias for that. Only thing is: the port sometime changes. So I did this in bash profile:

function runserver() {
   python manage.py runserver 10.211.55.4:$1
}

But then when I call it: runserver 3000, it starts it, but immediately stops saying:

"Error: That IP address can't be assigned-to". However if I type the same thing right into command line it works with no complains.

1
  • Sanity check: do you run bash --login all the time? Function definitions should be in .bashrc, not .bash_profile, since .bash_profile is only read by login shells. Jun 3, 2014 at 22:38

4 Answers 4

3

Along the lines of your original attempt, you could adjust that function to the following:

function runserver() {
    python manage.py runserver 10.211.55.4:$PORT
}

and then call it like so:

PORT=3000 runserver
5
  • Alternatively of course, you could always just whip up a quick bash or python script that takes your port argument, and stick it somewhere in your path. Jun 3, 2014 at 20:46
  • That wouldn't make any difference apart from the less convenient syntax. The command that's executed in the end is exactly the same. Jun 3, 2014 at 21:02
  • The trick is that his function definition is in his bash profile. $1 here refers to arguments given to the .bash_profile, not the function itself. Jun 3, 2014 at 21:45
  • 2
    This is wrong. $1 in a function definition refers to the argument of the function. Jun 3, 2014 at 22:37
  • I must admit you are correct, I was mistaken. Given that, I see nothing incorrect with the original function definition as provided in the question. Jun 3, 2014 at 23:02
3

This is actually an error you get back from Python/Django when you feed it an IP address it can't bind to (one that isn't part of the system) as shown in a real, just-run example below:

$ python manage.py runserver 123.123.123.123:8000
Validating models...

0 errors found
June 03, 2014 - 22:36:58
Django version 1.5.7, using settings 'tv.settings'
Development server is running at http://123.123.123.123:8000/
Quit the server with CONTROL-C.
Error: That IP address can't be assigned-to.

So you either need to fix your IP address (look at the output of ifconfig --all to see what's available) or you just bind to all of them with:

function runserver() {
    python manage.py runserver 0:$1
}

The bash side of things is sound.

0

You're doing it right.

I suspect that you're running into a networking issue in your testing, and the fact that the failed test used the function was a coincidence. When you close a TCP connection, the port remains in use for a small amount of time (30s), in case there were pending packets that were not yet received (packets may arrive out of order). This is the TIME_WAIT state. To allow a new server to restart immediately, set the SO_REUSEADDR flag with setsockopt (there's an example in the Python socket documentation).

1
  • but why does it work when I type the same thing right to the console?
    – iLemming
    Jun 3, 2014 at 21:27
0

We can make alias with group command { list; } and here-string <<< redirection. The key goal is to redirect stdin to the command. There's a couple of ways presented below

alias runserver='{ xargs -I % python manage.py runserver 10.211.55.4:%;} <<<'

alias runserver='{ IFS= read -r port; python manage.py runserver 10.211.55.4:"$port";} <<<'

alias runserver='{ port=$(line); echo python manage.py runserver 10.211.55.4:"$port";} <<<'

Note the space after { and semicolon before } are required.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .