Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Friday, January 25, 2019

Auto Login with Python + SSH + Expect

Python and Expect

#!/usr/bin/python                                                                                                                     
import argparse
from ConfigParser import ConfigParser
import pexpect

def main(args):
    url = args.url
    user, host = url.split('@', 1)

    cfg_file = '/home/user/bala_python.passwords'
    cfg = ConfigParser()
    cfg.read(cfg_file)
    passwd = cfg.get(user, host)

    child = pexpect.spawn('ssh {0}'.format(url))
    child.expect('password:')
    child.sendline(passwd)
    child.interact()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Run ssh through pexpect')
    parser.add_argument('url')
    args = parser.parse_args()
    main(args)

....
bala_python.passwords
[user1]
host1 = password1
host2 = password2

[user2]
host1 = password1
host2 = password2

Friday, July 15, 2016

Simple Python HTTPS Server

# Simple HTTP Server

$ python -m SimpleHTTPServer 8080

# Simple HTTPS Server

# generate server.xml with the following command:
$ openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
# run as follows:
$ python simple-https-server.py
# then in your browser, visit:
#    https://localhost:4443

import BaseHTTPServer, SimpleHTTPServer
import ssl

httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./server.pem', server_side=True)
httpd.serve_forever()


http://www.piware.de/2011/01/creating-an-https-server-in-python/

Wednesday, June 8, 2016

Simple Webserver to host/share files in Python

server.py

import SimpleHTTPServer
import SocketServer

PORT = 80

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()


Use sudo to use port 80
$ sudo python server.py


or
$ sudo yum install python-pip
#pip install SimpleHTTPServer
$ sudo python -m SimpleHTTPServer 80


#firefox http://localhost/

Thursday, May 26, 2016

Raspberry "Hello World" in C and Python

Connection to pins GND yellow wire 5th pin from left to right
and GPIO17 green wire 6th from left to right
 Circuit with led and a resistor (~330 Ohms) 



To use Python nothing have to be installed 
#!/usr/bin/python
from gpiozero import LED
from time import sleep
led = LED(17)
for num in range(1,10):
    led.on()
    sleep(1)
    led.off()
    sleep(1)
To use the C language, install the wiringPi library

# git clone git://git.drogon.net/wiringPi
# cd wiringPi
#./build

Pin 0 is the GPIO17
blink.c
#include <wiringPi.h>

#define LED 0

int main (void)
{
  int i ;
  wiringPiSetup() ;
  pinMode (LED, OUTPUT) ;
  for (i=0 ; i < 10; i++)
  {
    digitalWrite(LED, HIGH) ;
    delay(500) ;
    digitalWrite(LED, LOW) ;
    delay(500) ;
  }
  return 0 ;
}
# gcc -L/usr/local/lib -lwiringPi -lwiringPiDev -lpthread -lm -I/usr/local/include -Winline -pipe -o blink blink.c
# export WIRINGPI_GPIOMEM=1
# ./blink

WIRINGPI_GPIOMEM variable is to use the /dev/gpiomem instead of /dev/mem. Without this variable set you need root privilegies to execute the program
http://wiringpi.com
https://www.raspberrypi.org/learning/python-quick-reaction-game/worksheet


Pins for reference, in this example we use the pin G017 and Ground(GND)

Tuesday, March 9, 2010

Debugging in python

Pdb o gdb do python
http://docs.python.org/library/pdb.html

coloque o seguinte código na linha que quer colocar o breakpoint
import pdb; pdb.set_trace()

salve

rode o script python assim
python -m pdb myscript.py

(Pdb)>>> b :



Fazendo testes em python

http://docs.python.org/library/unittest.html

exemplo

import random
import unittest

class TestSequenceFunctions(unittest.TestCase):

def setUp(self):
self.seq = range(10)

def test_shuffle(self):
# make sure the shuffled sequence does not lose any elements
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, range(10))

def test_choice(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)

def test_sample(self):
self.assertRaises(ValueError, random.sample, self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)

if __name__ == '__main__':
unittest.main()

Blog Archive