Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
How to check whether a socks server working or not?

Let's say you have run a socks server on port 8888. Now in order to test the server, you just need to use cURL! You need to provide --socks5 parameter to cURL as below:
curl --socks5 localhost:8888 binfalse.de

If you see some HTML stuff, your server is up & running, but if you see the below error it seems the server is not working properly:
curl: (7) Failed to connect to localhost port 8888: Connection refused

The above error says that port 8888 is not available, you may see different errors based on the server configuration stat. Hope it helps.

#socks5 #socks #socks_server #curl
If you are working in docker and have utf8 problems like UnicodeDecodeError exceptions. And you are scratching your head like me and encode & decode does no good for the problem, use setdefaultencoding:

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

NOTE: it is discouraged to use the above function. As I have mentioned docker which its OS is isolated, I aware of what I'm doing. In case you are in doubt DO NOT USE IT specially on host OS!

#python #encoding #utf8 #encode #decode
As you may remember we have explained redis pubsub in which someone subscribe to a specific channel and then another console publish messages to that channel. The publisher has no idea who is listening on the other side, it just publish messages which is received.

Today we are gonna give a python example of the redis pubsub using threading and redis python module. Take a look at the code in the link below:

https://gist.github.com/alirezastack/ff2515cc434360f544d8a9341155947e

I prefer not to clutter the post by pasting the whole script here. :)

subscribe method is used to subscribe to a given channel, here it is called test. start method is used as part of the threading
module which causes run method to be called. Both run and start is Thread super class methods. When you run the script it will subscribe to test channel and wait for new messages.

NOTE: you can publish to test channel in redis console (`redis-cli`) as below:

127.0.0.1:6379> publish test hello_python
(integer) 1

Usecases are endless! You can use as part of messaging infrastructure in your microservice environment or as a chat system, you name it! :)

#python #redis #pubsub #publish #subscribe #thread
Executing MySQL statements from a text file:

In MySQL it is possible to put your SQL statements in a file and then tell mysql to read its input from that file. To do so, create a text file text_file that contains the statements you wish to execute. Then invoke mysql as shown here:

shell> mysql db_name < text_file

NOTE: If you place a USE db_name statement as the first statement in the file, it is unnecessary to specify the database name on the command line.


If you are already running mysql, you can execute an SQL script file using the source command or \. command:

mysql> source file_name


#mysql #execute #command #statement
How to add color to your logs in python?

It's easy as pie, just install coloredlogs with pip and then:

import coloredlogs, logging
logger = logging.getLogger(__name__)
coloredlogs.install(level='DEBUG')

# Some examples.
logger.debug("this is a debugging message")
logger.info("this is an informational message")
logger.warning("this is a warning message")
logger.error("this is an error message")
logger.critical("this is a critical message")

By default the install() function installs a handler on the root logger, this means that log messages from your code and log messages from the libraries that you use will all show up on the terminal.

If you don't want to see log messages from libraries, you can pass a specific logger object to the install() function. In this case only log messages originating from that logger will show up on the terminal:

coloredlogs.install(level='DEBUG', logger=logger)

#log #logger #coloredlogs #logging #color
In designing API SDKs and documentation it is really annoying to put a whole lot of time to design the documentation and/or designing SDKs in different languages! How do you create SDKs for C#, python, Go, .NET, etc when you are limited in resources? IT world has gone so far, that creation of SDKs for the API or creation of documentation (with great web UI) have been made automatic. No one need to interfere in between.

As well as other programs or languages it needs some specification that all needs to follow. OpenAPI is the thing!

What is OpenAPI?
The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to RESTful APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined, a consumer can understand and interact with the remote service with a minimal amount of implementation logic.

One of the great editors for designing the spec is swagger live editor:
- https://editor.swagger.io/

The full open specification of OpenAPI 3.0:
- https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md

I should again note that this is a wonderful spec and makes your life easier if you master the specification. A sample documentation of OpenAPI is Alopayk:
- https://docs.alopeyk.com/

#openapi #swagger #spec #restful #api #alopayk
Did you know that you can monitor redis commands live from within redis-cli console?

Go to redis client by typing redis-cli in terminal and then type monitor command and enter:

127.0.0.1:6379> monitor
OK
1514301845.678553 [0 127.0.0.1:59388] "COMMAND"
1514301859.676761 [0 127.0.0.1:59388] "HSET" "user" "name" "ali"

It will log everything that happens on your redis server.

#redis #redis_cli #cli #monitor
In case all of a sudden you fell on Outlook for mail templates (yes that sucks!), and you have problem for right to left languages like Farsi read on.

Outlook removes styles from your tags, you need to provide Outlook specific tags for direction named dir. It will solve your RTL problems:
<span dir="rtl"></span>

NOTE: styles like "direction:rtl" wont work on outlook. Beside direction for other mail clients you need to provide dir attribute.

#outlook #email #direction
Nearly all of you have created a python script throughout your career, in case you are a python programmer! Usually scripts take arguments, provide help, optional arguments and more.

Working with modules like optparse and argparse do the job, but they are not powerful enough in case your are designing a complex script like pip CLI script! click python module is at your service.


NOTE: The biggest difference between optparse and argparse is that optparse is deprecated since Python 3.2 and argparse is considered the standard for implementing CLIs in Python.



click do a similar task akin to optparse and argparse but in a nicer way by using decorators.

Take a look at the below github gist and let's discuss about different parts of it:
- https://gist.github.com/alirezastack/cccb70640c5e5881fa71b23966707f8f

The above gist is a sample weather app using python 3. SAMPLE_API_KEY is used to send requests to openweathermap API. current_weather is a regular method like other python methods.

The important part of script starts from @click.command(). This command defines the main method as a cli app. If you just put this command before your main method and run your script as below, it will prints help instruction of your script:
$ python cli.py --help
Usage: cli.py [OPTIONS]

Options:
--help Show this message and exit.

As you can see it prints some default help for your barebone script. 21st line of script is @click.argument('location') that defines a required parameter for you CLI app called location.

If you want to define an optional argument use @click.option. As you can note we have used --api-key, -a in the same argument. It helps users to use the shortcut version or human readable format of the argument. Both refers to the same argument and it will be mapped to api_key method variable (snake case).

help in click.option is used to give a brief guide for the argument when --help is used.

Finally the docstring inside of main method which is inside of triple quote will be shown when --help is used.

NOTE: snake case is a procedure that removes dashes from the begining of an input argument and turns dash into underscore. So something like --format-type will be converted to format_type.

That was easy right? I know, it was super simple compared to argparse and optparse. Rock on!

#python #python3 #cli #click #argparse #optparse #argument