Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, December 11, 2021

netmiko: Multi-vendor network device library for python

 github.com/ktbyers/netmiko Multi-vendor library to simplify CLI connections to network devices; can be helpful espacially for SOAR projects

Monday, July 15, 2019

pyimagesearch

If you’re interested in learning how image search engines, or trying to build one of your own, you should check it out




https://www.pyimagesearch.com/

pyenv installation

Install:

$ curl https://pyenv.run | bash

pyenv.run redirects to the install script in this repository and the invocation above is equivalent to:

$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

Details:
https://github.com/pyenv/pyenv-installer

Thursday, October 11, 2018

Building Beautiful Command Line Interfaces with Python

https://codeburst.io/building-beautiful-command-line-interfaces-with-python-26c7e1bb54df

Monday, October 1, 2018

Using websockets to easily build GUIs for Python programs

Websocket teknolojisinin kullanımı ile alakalı faydalı bir örnek.

https://gist.github.com/jsomers/d32dd3507e5406c56e47b4cd6f28c60e

Friday, January 20, 2017

Python SimpleHTTPServer ile dizin servisi

Bulunduğunuz dizini spesifik bir porta atayacağınız SimpleHTTPServer  ile hızlıca servis edebilirsiniz. Özellikle hızlıca ağ içi dosya transferi için kullanışlı.

$ python -m SimpleHTTPServer 8090


Monday, September 28, 2015

python: multicast echo server


python: multicast echo server

code:

#!/usr/bin/python

##
# Author: Ali Okan Yuksel
# E-mail: okan.yuksel[at]vizyonarge.com
##


import socket
import struct
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', 1111))
mreq = struct.pack("=4sl", socket.inet_aton("226.2.14.8"), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

while True:
  line=sock.recv(10240)
  print line
  print "--"

Wednesday, September 16, 2015

python traceback module usage

In Python 2.x:
import traceback

try:
    raise TypeError("Oups!")
except Exception, err:
    try:
        raise TypeError("Again !?!")
    except:
        pass

    traceback.print_exc()


Python 3.x :
import traceback

try:
    raise TypeError("Oups!")
except Exception as err:
    try:
        raise TypeError("Again !?!")
    except:
        pass

    traceback.print_tb(err.__traceback__)

Thursday, July 30, 2015

python: reading fifo example


fifo - first-in first-out special file, named pipe

A FIFO special file (a named pipe) is similar to a pipe, except that
       it is accessed as part of the filesystem.  It can be opened by
       multiple processes for reading or writing.  When processes are
       exchanging data via the FIFO, the kernel passes all data internally
       without writing it to the filesystem.  Thus, the FIFO special file
       has no contents on the filesystem; the filesystem entry merely serves
       as a reference point so that processes can access the pipe using a
       name in the filesystem.

SEE ALSO  




fifo reader code:

#!/usr/bin/python


__author__ = "Ali Okan Yuksel <okan@siyahsapka.org>"

import sys



def main():
        print "INFO application started"
        fifo="okantest.fifo"
        fd=open(fifo,"r")
        while True:
                line = fd.readline()
                if line !="":
                        sys.stdout.write(line)


if __name__=="__main__":
        main()


data sender bash call:

[root@logserver ~]# for i in `seq 1 1000`; do echo $i > /opt/proxy/okantest.fifo ; done

Tuesday, March 17, 2015

Sunday, January 25, 2015

python: web scraping with BeautifulSoup

BeautifulSoup html, xml parse işlemleri için kullanabileceğiniz bir modül. iterating, searching, ve modifying işlemleri gerçekleştirebilirsiniz.

https://pypi.python.org/pypi/beautifulsoup4/4.3.2 adresinden temin edilebilir.

Xubuntu ortamında kurulumu pip ile aşağıdaki şekilde gerçekleştirdim.
Testlerimde Python 2.7.6 (default, Mar 22 2014, 22:59:56) sürümü kullandım.


Kurulum:

aokan$ sudo pip install beautifulsoup4

Örnekler:
#!/usr/bin/python
from bs4 import BeautifulSoup, NavigableString
import urllib2

url="http://www.siyahsapka.org"
soup=BeautifulSoup(urllib2.urlopen(url).read())
# style tanimi olan span'ların style'larini temizle
for span in soup('span',style=True):
    del span['style']


# tekrar eden break tag'larini (<br> veya <br />) teke indir. arada strong olsa dahi calis.
for br in soup('br'):
    sibling = br.next_sibling
    while sibling:
        if isinstance(sibling, NavigableString):
            sibling = sibling.next_sibling
            continue
        if sibling.name == 'br':
            sibling.decompose()
        elif sibling.name != 'strong':
            break
        sibling = sibling.next_sibling

# strong tag'i arasinda bulunan break (<br> veya <br />) ifadelerini temizle.
for strong in soup('strong'):
    for br in strong('br'):
        br.decompose()


# <a tag'i ile belirtilen link'leri  sadece icerisindeki text kalacak sekilde temizle.
for a in soup('a'):
    a.unwrap()

# gelen html'de yer alan <iframe tag ve içeriğini temizle.
for iframe in soup('iframe'):
    iframe.decompose()

# formatlama isleminden sonra istedigim div icindeki veriyi al
content=unicode(soup.find('div',attrs={'class':'txtIn'}))

Detaylar için http://www.crummy.com/software/BeautifulSoup/bs4/doc/



Eğer biçimlendireceğiniz html veya url sayısı 1000 üzerinde ise bu işlemleri multithread yapmanıza imkan sağlayan scrapy ile gerçekleştirmek daha doğru olacaktır.

Saturday, January 17, 2015

HTTP for Great Good


 https://speakerdeck.com/mattrobenolt/http-for-great-good


https://speakerdeck.com/mattrobenolt/cheating-your-way-to-webscale


Developing & Deploying "Large" Scale Web Applications

https://speakerdeck.com/mattrobenolt/developing-deploying-large-scale-web-applications 

How to Deploy Python WSGI Applications Using a CherryPy Web Server Behind Nginx

https://www.digitalocean.com/community/tutorials/how-to-deploy-python-wsgi-applications-using-a-cherrypy-web-server-behind-nginx

Faker: a Python package that generates data for you.

Faker is a Python package that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.

https://github.com/joke2k/faker

Basic Usage

    from faker import Factory
    fake = Factory.create()

    # OR
    from faker import Faker
    fake = Faker()

    fake.name()
    # 'Lucy Cechtelar'

    fake.address()
    # "426 Jordy Lodge
    #  Cartwrightshire, SC 88120-6700"

    fake.text()
    # Sint velit eveniet. Rerum atque repellat voluptatem quia rerum. Numquam excepturi
    # beatae sint laudantium consequatur. Magni occaecati itaque sint et sit tempore. Nesciunt
    # amet quidem. Iusto deleniti cum autem ad quia aperiam.
    # A consectetur quos aliquam. In iste aliquid et aut similique suscipit. Consequatur qui
    # quaerat iste minus hic expedita. Consequuntur error magni et laboriosam. Aut aspernatur
    # voluptatem sit aliquam. Dolores voluptatum est.
    # Aut molestias et maxime. Fugit autem facilis quos vero. Eius quibusdam possimus est.
    # Ea quaerat et quisquam. Deleniti sunt quam. Adipisci consequatur id in occaecati.
    # Et sint et. Ut ducimus quod nemo ab voluptatum.

Inject arbitrary code into a running Python process

$ pyrasite-shell $(pgrep -f "ipython")
Pyrasite Shell 2.0beta9
Connected to 'ipython'
Python 2.7.2 (default, Oct 27 2011, 01:40:22)
[GCC 4.6.1 20111003 (Red Hat 4.6.1-10)] on linux2
>>> print(x)
foo

>>> globals()['x'] = 'bar'


https://pyrasite.readthedocs.org/en/latest/index.html

random string generation with python

#!/usr/bin/python2.7

import random, string, sys
length=10
if len(sys.argv)>1:
        length=sys.argv[1]
rand_str = ''.join(random.choice(
                    string.ascii_lowercase
                    + string.ascii_uppercase
                    + string.digits)
               for i in range(int(length)))
print rand_str