Tumgik
dev-chommik · 5 years
Text
fun fact about TCP
There's one less-than-expected thing about TCP: even if you provide a small buffer to recv() and the other side sends exactly the same amount of data in the send() call, there's no guarantee that the data is received in one call.
Take for example Python:
# sock is a socket.socket(), already connected data = socket.recv(4) length = struct.unpack('=l', data)[0] # this might fail!
There are two ways to mitigate it:
really portable way: receive in a loop, until all data are received -- but beware of recv() returning b'' - this means there's an error in the communication
Linux/Unix way: use MSG_WAITALL flag to force recv() to block until whole buffer is filled
1 note · View note
dev-chommik · 7 years
Text
convert glob-style pattern to regular expression in python
Python's fnmatch library makes it easy to convert human-readable glob pattern to re.match()-compatible regexps:
% python3 >>> import fnmatch >>> fnmatch.translate('dupa.[ch]pp.*') 'dupa\\.[ch]pp\\..*\\Z(?ms)' >>>
0 notes
dev-chommik · 8 years
Text
split string in python
Python splits string via str.split() and str.rsplit() as well, the latter of which starts splitting from the end of the string. This can be useful when using the second parameter, maxsplit.
For example:
>>> "string with multiple spaces".split(" ", maxsplit=1) ['string', 'with multiple spaces']
This parameter can be used e.g. to split command and arguments apart.
0 notes
dev-chommik · 8 years
Text
find DNS servers in nmcli
NetworkManager usually puts "127.0.1.1" in /etc/resolv.conf. To get currently used DNS servers, type:
% nmcli d show | grep DNS IP4.DNS[1]: 10.1.2.3 IP4.DNS[2]: 10.2.3.4
0 notes
dev-chommik · 8 years
Text
quickly open links in Terminator
Instead of using Open link from context menu, you can click the link while holding Control key.
0 notes
dev-chommik · 8 years
Text
search backwards in less
In addition to searching forwards (/pattern), you can search backwards: ?pattern
0 notes
dev-chommik · 8 years
Text
print strace summary
To summarise syscalls by time or count, use strace -c. To include child processed, add -f switch. For example:
λ strace -cf git status % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 100.00 0.000008 0 247 24 lstat 0.00 0.000000 0 48 read 0.00 0.000000 0 3 write ... list goes on ...
0 notes
dev-chommik · 8 years
Text
vim: run command without exiting Insert
In Vim, you can exist Insert mode to enter one command using Ctrl-O, for example: (in insert) Ctrl-O, type :w, then ↵ Enter, then back to writing.
0 notes
dev-chommik · 8 years
Text
vim: increment/decrement number under cursor
To quickly in-/decrement number under cursor in Vim, use (in Normal):
to increment: Ctrl-A
to decrement: Ctrl-X
0 notes
dev-chommik · 8 years
Text
shorter for-loops in zsh
When running a for-loop in zsh, you can skip do-done part, leaving only command, for example:
% for day in Mon Tue Wed Thu; date -d "next $day" Mon Apr 25 00:00:00 CEST 2016 Tue Apr 26 00:00:00 CEST 2016 Wed Apr 27 00:00:00 CEST 2016 Thu Apr 28 00:00:00 CEST 2016
Caveat: this doesn't work on multiple commands in one loop — for day in Mon Tue Wed Thu; echo $day; date -d "next $day" won't work as expected
0 notes
dev-chommik · 8 years
Text
vim: switch case
In Normal mode, use ~ to switch case of letter under cursor. Similar commands are:
gU – to switch to upper-case
gu – to switch to lower-case
For example: gUU converts whole line to upper-case and guiw to convert word surrounder by cursor to lower-case.
0 notes
dev-chommik · 8 years
Text
list mounts by fs type
You can list mountpoints by file system type using mount -tTYPE. Example:
# mount -ttmpfs tmpfs on /dev/shm type tmpfs (rw) tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
0 notes
dev-chommik · 9 years
Text
install rpm by url
For those Debian-inclined, this may be surprising. You can install RPMs by providing an URL to either yum or rpm -i:
rpm -i https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
0 notes
dev-chommik · 9 years
Text
automatically set prompt colour based on hostname
When logging on multiple machines with synchronized ~/.bashrc / ~/.zshrc you probably don't want to set PS1 on each machine separately. Solution based on hostname will be handy.
To derive user@hostname (or any other host-identifying command) into uniformly-distributed colours, use:
_host_colour=$(echo $(whoami)@$(hostname -f) | sum | awk "{print \$1 % 256 }")
GNU tool sum generates simple, short checksum which are then shortened down to 256 (the default Xterm colour count). Now add the variable to your PS1:
# Bash export PS1="\[\033[38;5;\${_host_colour}m\]\u@\h\[\033[0m\] % " # Zsh (requires module colors) export PS1="%F{\${_host_colour}}%n@%m%f %% "
Protip: If you want to change colour per-host, just set variable _host_colour. You can do it even after PS1 was set.
0 notes
dev-chommik · 9 years
Text
bash traps
Bash traps override rather than stack. Example:
λ trap 'echo A' SIGINT λ trap 'echo B' SIGINT λ trap 'echo C' SIGINT λ kill -SIGINT $$ C
0 notes
dev-chommik · 9 years
Text
search in aptitude
You can use regular expressions in aptitude search. Example:
^node$
will search for the whole package name (in this case: npm)
0 notes
dev-chommik · 9 years
Text
variable names in $(( expr ))
You can use variable names without $ in bash:
a=3 echo $(( a - 1 ))
Note on compatibility: in Bash, you have to prefix special variables like $?, unlike Zsh.
0 notes