Categories
Python Answers

How to get IP address of visitors using Flask for Python?

Sometimes, we want to get IP address of visitors using Flask for Python.

In this article, we’ll look at how to get IP address of visitors using Flask for Python.

How to get IP address of visitors using Flask for Python?

To get IP address of visitors using Flask for Python, we can use the request.remote_addr property.

For instance, we write

from flask import request
from flask import jsonify

@app.route("/get_my_ip", methods=["GET"])
def get_my_ip():
    return jsonify({'ip': request.remote_addr}), 200

to get the client’s IP address with the request.remote_addr property.

Conclusion

To get IP address of visitors using Flask for Python, we can use the request.remote_addr property.

Categories
Python Answers

How to access the query string in Flask routes with Python?

Sometimes, we want to access the query string in Flask routes with Python.

In this article, we’ll look at how to access the query string in Flask routes with Python.

How to access the query string in Flask routes with Python?

To access the query string in Flask routes with Python, we can use the request.query_string property.

For instance, we write

from flask import request

@app.route('/adhoc_test/')
def adhoc_test():

    return request.query_string

to get the query string with request.query_string within the adhoc_test view.

We use request.args.get('param') to get the value of the param query string parameter.

Conclusion

To access the query string in Flask routes with Python, we can use the request.query_string property.

Categories
Python Answers

How to open a file for both reading and writing with Python?

Sometimes, we want to open a file for both reading and writing with Python.

In this article, we’ll look at how to open a file for both reading and writing with Python.

How to open a file for both reading and writing with Python?

To open a file for both reading and writing with Python, we can call open with the 'r+' permission.

For instance, we write

with open(filename, "r+") as f:
    data = f.read()
    f.seek(0)
    f.write(output)
    f.truncate()

to call open with the filename and the 'r+' permission to open the file at filename with read and write permission.

Then we call read to read the file in the with block.

And we call write to write the output to the file.

Conclusion

To open a file for both reading and writing with Python, we can call open with the 'r+' permission.

Categories
Python Answers

How to encode a string according to a password with Python?

Sometimes, we want to encode a string according to a password with Python.

In this article, we’ll look at how to encode a string according to a password with Python.

How to encode a string according to a password with Python?

To encode a string according to a password with Python, we can use the cryptography library.

To install it, we run

pip install cryptography

Then we use it by writing

from cryptography.fernet import Fernet

key = Fernet.generate_key() 

from cryptography.fernet import Fernet

def encrypt(message: bytes, key: bytes) -> bytes:
    return Fernet(key).encrypt(message)

def decrypt(token: bytes, key: bytes) -> bytes:
    return Fernet(key).decrypt(token)

message = 'John Doe'
token = encrypt(message.encode(), key)

decoded = decrypt(token, key).decode()

to create the encryption key with generate_key.

And then we call encrypt in the encrypt function to encrypt the message byte string with the key.

In the decrypt function, we call decrypt with the token byte string to decrypt the string with the key.

And then we call decode to decode the byte string into the original string.

Conclusion

To encode a string according to a password with Python, we can use the cryptography library.

Categories
Python Answers

How to get distance between two points based on latitude and longitude with Python?

Sometimes, we want to get distance between two points based on latitude and longitude with Python.

In this article, we’ll look at how to get distance between two points based on latitude and longitude with Python.

How to get distance between two points based on latitude and longitude with Python?

To get distance between two points based on latitude and longitude with Python, we can use the geopy.distance module.

To install it, we run

pip install geopy

Then we use it by writing

import geopy.distance

coords_1 = (52.2296756, 21.0122287)
coords_2 = (52.406374, 16.9251681)

print(geopy.distance.geodesic(coords_1, coords_2).km)

to get the distance between coords_1 and coords_2 with the geodesic method.

And we get the distance in kilometers with the km property.

Conclusion

To get distance between two points based on latitude and longitude with Python, we can use the geopy.distance module.