Join bytes in Python
def run_server():
HOST = ''
PORT = 11000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(10)
while True:
conn, addr = s.accept()
print("{0} - ".format(get_counter()) + 'Connection from : {0}'.format(addr))
data_recv=conn.recv(1024)
data=bytearray()
conn.settimeout(1)
while len(data_recv)>0:
data.extend(data_recv)
try:
data_recv=conn.recv(1024)
except socket.timeout:
break
data=str(data)
if data=="1":
message=get_message1()
conn.sendall(message)
conn.close()
References :
http://stackoverflow.com/questions/9099145/where-are-python-bytearrays-used
Bytes stream in Python
@staticmethod
def from_bin(dataB):
reader=io.BytesIO(dataB)
length1= reader.read(4)
length2=reader.read(4)
length3=reader.read(4)
macs=reader.read(length1)
uid=reader.read(length2)
signature=reader.read(length3)
reader.close()
message=Message(macs,uid,signature)
return message
References :
https://docs.python.org/3/library/io.html#io.BytesIO http://chimera.labs.oreilly.com/books/1230000000393/ch05.html#_solution_78
https://docs.python.org/2/library/io.html
Serialize memory objects to binary in Python
try:
import cPickle as pickle
except:
import pickle
import pprint
data1 = [ { 'a':'A', 'b':2, 'c':3.0 } ]
print 'BEFORE:',
pprint.pprint(data1)
data1_string = pickle.dumps(data1)
data2 = pickle.loads(data1_string)
print 'AFTER:',
pprint.pprint(data2)
print 'SAME?:', (data1 is data2)
print 'EQUAL?:', (data1 == data2)
References:
https://docs.python.org/3/library/pickle.html
https://docs.python.org/2/library/pickle.html
https://pymotw.com/2/pickle/
Broadcast address
IP
The broadcast address for an IPv4 host can be obtained by performing a bitwise OR operation between the bit complement of the subnet mask and the host’s IP address. In other words, take the host’s IP address, and set to ‘1’ any bit positions which hold a ‘0’ in the subnet mask.
Example: For broadcasting a packet to an entire IPv4 subnet using the private IP address space 172.16.0.0/12, which has the subnet mask 255.240.0.0, the broadcast address is 172.16.0.0 | 0.15.255.255 = 172.31.255.255.
255.240.0.0=11111111.11110000.00000000.00000000 172.16.0.0=10101100.00010000.00000000.00000000 10101100.00011111.11111111.11111111=172.31.255.255
Ethernet
Broadcast is possible also on the underlying Data Link Layer in Ethernet networks. Frames are addressed to reach every computer on a given LAN segment if they are addressed to MAC address FF:FF:FF:FF:FF:FF. Ethernet frames that contain IP broadcast packages are usually sent to this address.
References :
colorama – Python Package
Description:
Cross-platform colored terminal text
Available formatting constants are:
Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. Style: DIM, NORMAL, BRIGHT, RESET_ALL
Example :
from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')
References :
https://pypi.python.org/pypi/colorama
http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
Native file system watcher for Linux
for using with Jetbrains product like pycharm and idea :
The current limit can be verified by executing:
cat /proc/sys/fs/inotify/max_user_watches
It can be raised by adding following line to the /etc/sysctl.conf file:
fs.inotify.max_user_watches = 524288
or
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
… and issuing this command to apply the change:
sudo sysctl -p
Check if there is a file in
/etc/sysctl.dwith your parameter. These files override the/etc/sysctl.conffile
References
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers
https://serverfault.com/questions/355520/after-reboot-debian-box-ignore-sysctl-conf-values
Ubuntu Multiarch
sudo dpkg --add-architecture i386
How to Disable, Stop or Uninstall Apport Error Reporting
Stop Apport
sudo service apport stop
Disable Apport at Boot
sudo nano /etc/default/apport
Uninstall Apport
sudo nano /etc/default/apport
ShadowSocks PPA
sudo add-apt-repository ppa:hzwhuang/ss-qt5