Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
If you want to monitor a specific tcp port on Icinga2 you just need to add another tcp variable:
object Host "host-web" {
import "generic-host-tpl"

address = "YOUR_SERVER_IP_ADDRESS"
vars.os = "Linux"

vars.tcp["OPTIONAL_NAME"]={
tcp_port=8181
}

Here in above sample we are monitoring port 8181 of host-web which has the IP address of YOUR_SERVER_IP_ADDRESS (change it to your server ip address remote or local).

#linux #monitoring #sysadmin #icinga2 #host #tcp #port
In order to remove a specific document in couchDB, you just need to give delete function the document itself:
couch = couchdb.Server('http://SERVER_IP:PORT/')
your_db = couch['your_db_name']

# we assume you have fetched your_couch_db_doc document
your_db.delete(your_couch_db_doc)

Use this method in preference over __del__ to ensure you're deleting the revision that you had previously retrieved. In the case the document has been updated since it was retrieved, this method will raise a ResourceConflict exception:
>>> db.delete(doc) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ResourceConflict: (u'conflict', u'Document update conflict.')

#couchdb #couch #delete #remove #document #python
If you want to find an element inside of a list in MongoDB document you can use the simple find command. Let's say you have the below documents:
[{
"food": "Ghorme",
"creation_date": "2017-10-12 12:00:00"
"rates": ["bad", "so_so", "not_bad", "mmmm"]
},{
"food": "Kebab",
"creation_date": "2017-10-07 22:10:00"
"rates": ["great", "not_bad", "great", "great"]
}]

Now if you want to find a document that has the value of bad in rates, you just use the below find query:
db.foods.find({rates: "bad" }).pretty()

#mongodb #mongo #find #list #array
If you want to undo something in vim use u key and in case you want to redo an action just press ^r.

#vim #undo #redo
Tech C**P
Photo
Setup a proxy server by using SwitchyOmega chrome addon. Download and install it from chrome store and setup the credential as picture. Use ssh -D 5300 USERNAME@YOUR_SERVER_IP to proxy your browser traffic (server should be located in Europe or America).

#proxy #switchy_omega #ssh #tunnel #socks
How to use if in jinja2 template engine?

Jinja2 is a modern and designer-friendly templating language for Python, modelled after Django’s templates. It is fast, widely used and secure.

If you want to use conditional statements like if inside of template engine you should use a syntax like below:
{% if status == 'success' %}
Some text for successful procedure
{% else %}
Some text for not successful procedure
{% endif %}

#python #jinja2 #template_engine #template
How do I remove the passphrase for the SSH key without having to create a new key?

I set a passphrase when creating a new SSH key on my laptop. But, as I realise now, this is quite painful when you are trying to commit (Git and SVN) to a remote location over SSH many times in an hour.
$ ssh-keygen -p
Enter file in which the key is (/Users/my_username/.ssh/id_rsa):
Enter old passphrase:
Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.

This will then prompt you to enter the keyfile location, the old passphrase, and the new passphrase (`which can be left blank to have no passphrase`).

If you would like to do it all on one line without prompts do:
$ ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]

NOTE: Beware that when executing commands they will typically be logged in your ~/.bash_history file (or similar) in plain text including all arguments provided (i.e. the passphrases in this case). It is therefore is recommended that you use the first option unless you have a specific reason to do otherwise.

You might want to consider using ssh-agent, which can cache the passphrase for a time. The latest versions of gpg-agent also support the protocol that is used by ssh-agent.

#id_rsa #ssh #passphrase #keygen #linux #osx #remove_passphrase
To set a variable in jinja2 you can use set as below:
{% set pages_title = 'Pages' %}

As you notice we have used {% %} to set a variable not { }.

#variable #define #jinja2 #template
Dear readers, tonight I do my best to publish the first video for DJANGO tutorials in Persian.
Stay tuned...
If you work with Flask and have an under heavy load API, you can setup a cache for your endpoints. One of the tools is Flask- Cache that can work with different cache backends like Memcached, Redis, Simple, etc.

To install it:
pip install Flask-Cache

To setup up:
from flask import Flask
from flask.ext.cache import Cache

app = Flask(__name__)
# Check Configuring Flask-Cache section for more details
cache_config = {
"CACHE_TYPE": "redis",
"CACHE_REDIS_HOST": "127.0.0.1",
"CACHE_REDIS_PORT": 6379,
"CACHE_REDIS_DB": 3
}
cache = Cache(app,config=cache_config})

Another redis implementation:
#: the_app/custom.py
class RedisCache(BaseCache):
def __init__(self, servers, default_timeout=500):
pass

def redis(app, config, args, kwargs):
args.append(app.config['REDIS_SERVERS'])
return RedisCache(*args, **kwargs)

Now to use the cache on a method use its decorator:
@cache.memoize(timeout=50)
def big_foo(a, b):
return a + b + random.randrange(0, 1000)

#python #flask #cache #redis #memcached
http://pygments.org/

This is a syntax highlighter that creates permanent link for your code in a variety of languages including python in the provided link.

#python #pip #syntax_highlighter #highlighter #pygments
clone is used in git to copy a project into your machine as a git project and do you works on it. Sometime a project (specially front projects) are so heavy and has lots of history which makes cloning to takes an hour or more (depending on the depth and the size of the repo).

The solution is --depth, with --depth you can specify how shallow a clone could be and how much commit of the past should be brought into your system. So for example you can clone like this:
git clone myhost:frontier/web.git --depth=1
It will copy the whole project BUT it just copies the last commit on the tip of the current branch in your server (most likely master) which is the default behaviour of git that set --single-branch. So if you checkout to dev you wont see your last changes
in dev branch. In case you want to shallow copy the whole project and retrieve the lat commit on the tip of all remote branches just use --no-single-branch.

So finally we can:
git clone myhost:frontier/web.git --depth=1 --no-single-branch

Now if you change your branch (checkout) to dev, you will see that recent changes of the dev branch on the remote repo server is present in your system.

to see the last commit ids that you have in your system, open YOUR_PROJECT/.git/shallow file and see the content of the file. Mine is as below:
8252b87c82b4be7b7b4edaa12f2168ff165fc7af #refers to my master last commit id
d50bdeeecc595e86818c68d734613542206bf972 #refers to my dev last commit id

#git #branch #no-single-branch #single-branch #depth #clone
format() is way better than % in many different ways. One of those is:
>>> names = ['ali', 'reza', 'alireza', 'mohsen']
>>> print map('Hello Mr. {}'.format, names)
['Hello Mr. ali', 'Hello Mr. reza', 'Hello Mr. alireza', 'Hello Mr. mohsen']

#python #format #string_formatter #map
Tech C**P
format() is way better than % in many different ways. One of those is: >>> names = ['ali', 'reza', 'alireza', 'mohsen'] >>> print map('Hello Mr. {}'.format, names) ['Hello Mr. ali', 'Hello Mr. reza', 'Hello Mr. alireza', 'Hello Mr. mohsen'] #python #format…
format() is way better than %:
>>> a = {'name': 'ali', 'credit': '$2.0', 'status': 'active'}
>>> 'User {name} is {status} and has balance equal to {credit}'.format(**a)
'User ali is active and has balance equal to $2.0'
Media is too big
VIEW IN TELEGRAM
Django interactive shell

#python #django #shell #django_part7