• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Search

python

Python Web Server

May 20, 2022

Here’s a quick way to start a web server using Python (not recommended for production).

If you want to use python3, you may have to use explicitly use python3.

Check what versions you have first.

python --version
python3 --version

python --version python3 --version

Start web server using port 8080 in the current directory.

python -m http.server 8080

python -m http.server 8080

If you want an alternate directory, use this -d option.

python -m http.server 8080 -d /tmp

python -m http.server 8080 -d /tmp

It’s a great tool for quick downloads.

Filed Under: Linux Tagged With: download, python, server, web

Python Boto3 S3 List Objects

February 27, 2022

How to list S3 objects using Python Boto3

import boto3
from sys import argv
bucket=argv[1]
s3 = boto3.resource('s3')
mybucket = s3.Bucket(bucket)
def list_buckets():
    print("List of Buckets: ")
    for bucket in s3.buckets.all():
        print(bucket.name)
def list_files():
    print("Buckets: The objects in the \"" + bucket + "\" buckets are ... ")
    for files in mybucket.objects.all():
        print(files.key)
def main():
    #list_buckets()
    list_files()
if __name__ == "__main__":
    main()

import boto3 from sys import argv bucket=argv[1] s3 = boto3.resource('s3') mybucket = s3.Bucket(bucket) def list_buckets(): print("List of Buckets: ") for bucket in s3.buckets.all(): print(bucket.name) def list_files(): print("Buckets: The objects in the \"" + bucket + "\" buckets are ... ") for files in mybucket.objects.all(): print(files.key) def main(): #list_buckets() list_files() if __name__ == "__main__": main()

Running command

$ python test.py bucket-name    # with python
$ ./test.py bucket-name         # without python

$ python test.py bucket-name # with python $ ./test.py bucket-name # without python

Results

Buckets: The objects in the "bucket-name" are ....
abc.txt
def.txt

Buckets: The objects in the "bucket-name" are .... abc.txt def.txt

Filed Under: Linux Tagged With: boto3, list, objects, python, s3

Python Virtual Environment

February 27, 2022

How to create a Python virtual environment.

On Windows

$ pip install venv
$ python3 -m venv ~/python-test

$ pip install venv $ python3 -m venv ~/python-test

On Ubuntu

$ sudo apt install python3.8-venv
$ python3 -m venv ~/python-test

$ sudo apt install python3.8-venv $ python3 -m venv ~/python-test

On Mac OS

$ brew install virtualenv
$ virtualenv python-test

$ brew install virtualenv $ virtualenv python-test

Activate

$ cd ~/python-test
$ source bin/activate

$ cd ~/python-test $ source bin/activate

Deactivate

$ deactivate

$ deactivate

Filed Under: Linux Tagged With: activate, deactivate, environment, python, virtual

Json Formatter

January 12, 2022

If you have a json file that needs formatting, you can use a built-in tool in Python.

python -m json.tool sample.json

python -m json.tool sample.json

Output will be printed on screen. You can also send the output to a file.

python -m json.tool sample.json > sample2.json

python -m json.tool sample.json > sample2.json

Filed Under: Linux Tagged With: formatter, json, python

Fibonacci Sequence in Python

December 3, 2021

How to calculate Fibonacci sequence in Python.

Create a file called fib.py.

nterms = int(input("How many terms? "))
n1, n2 = 0, 1
count = 0
if nterms <= 0:
    print("Please enter a positive integer")
elif nterms == 1:
    print("Fibonacci sequence:")
    print(n1)
else:
    print("Fibonacci sequence:")
    while count < nterms:
        print(n1)
        nth = n1 + n2
        # update values
        n1 = n2
        n2 = nth
        count += 1

nterms = int(input("How many terms? ")) n1, n2 = 0, 1 count = 0 if nterms <= 0: print("Please enter a positive integer") elif nterms == 1: print("Fibonacci sequence:") print(n1) else: print("Fibonacci sequence:") while count < nterms: print(n1) nth = n1 + n2 # update values n1 = n2 n2 = nth count += 1

Run it.

$ python fib.py
How many terms? 5
Fibonacci sequence:
0
1
1
2
3

$ python fib.py How many terms? 5 Fibonacci sequence: 0 1 1 2 3

Again.

$ python fib.py
How many terms? 10
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34

$ python fib.py How many terms? 10 Fibonacci sequence: 0 1 1 2 3 5 8 13 21 34

Filed Under: Linux Tagged With: fibonacci, math, python, sequence

  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to Next Page »
  • Home
  • About
  • Search

Copyright © 2023