xxxxxxxxxx
Depending on what you are doing, system() or popen() may be perfect. Use system() if the Python script has no output, or if you want the Python script's output to go directly to the browser. Use popen() if you want to write data to the Python script's standard input, or read data from the Python script's standard output in php. popen() will only let you read or write, but not both. If you want both, check out proc_open(), but with two way communication between programs you need to be careful to avoid deadlocks, where each program is waiting for the other to do something.
If you want to pass user supplied data to the Python script, then the big thing to be careful about is command injection. If you aren't careful, your user could send you data like "; evilcommand ;" and make your program execute arbitrary commands against your will.
escapeshellarg() and escapeshellcmd() can help with this, but personally I like to remove everything that isn't a known good character, using something like
preg_replace('/[^a-zA-Z0-9]/', '', $str)
xxxxxxxxxx
import requests
import json
data = {
"api_key": "PRIVATE_API_KEY",
"profiles": [
{
"$consent": ["sms"],
"phone_number": "+12345678900",
"sms_consent": True
}
]
}
headers = {
"Content-Type": "application/json",
"Cache-Control": "no-cache"
}
conv = json.dumps(data)
response = requests.request("POST", "https://a.klaviyo.com/api/v2/list/{LIST_ID}/subscribe", data=conv, headers=headers)
print(response.text)
xxxxxxxxxx
import requests
import json
data = {
"api_key": "PRIVATE_API_KEY",
"profiles": [
{
"$consent": ["sms"],
"phone_number": "+12345678900",
"sms_consent": True
}
]
}
headers = {
"Content-Type": "application/json",
"Cache-Control": "no-cache"
}
conv = json.dumps(data)
response = requests.request("POST", "https://a.klaviyo.com/api/v2/list/{LIST_ID}/subscribe", data=conv, headers=headers)
print(response.text)
xxxxxxxxxx
def no_of_notes(amt, note):
n = [0, 0, 0]
min_amt = 0
for i in note:
min_amt += i
if amt < min_amt:
i = 0
while amt != 0:
n1 = amt // note[i]
amt = amt - (n1 * note[i])
n[i] += n1
i += 1
else:
n = [1, 1, 1]
if amt == min_amt:
return n
i = 0
amt -= min_amt
while amt != 0:
n1 = amt // note[i]
amt = amt - (n1 * note[i])
n[i] += n1
i += 1
return n
Amount = int(input('Enter a price: '))
note = [500, 200, 100]
result = no_of_notes(Amount, note)
number_of_notes = 0
for i in range(len(note)):
if result[i] != 0:
print(f' Rs.{note[i]} notes = {result[i]}')
number_of_notes += result[i]
print(f'Total number of notes = {number_of_notes}')
xxxxxxxxxx
def no_of_notes(amt, note):
n = [0, 0, 0]
min_amt = 0
for i in note:
min_amt += i
if amt < min_amt:
i = 0
while amt != 0:
n1 = amt // note[i]
amt = amt - (n1 * note[i])
n[i] += n1
i += 1
else:
n = [1, 1, 1]
if amt == min_amt:
return n
i = 0
amt -= min_amt
while amt != 0:
n1 = amt // note[i]
amt = amt - (n1 * note[i])
n[i] += n1
i += 1
return n
Amount = int(input('Enter a price: '))
note = [500, 200, 100]
result = no_of_notes(Amount, note)
number_of_notes = 0
for i in range(len(note)):
if result[i] != 0:
print(f' Rs.{note[i]} notes = {result[i]}')
number_of_notes += result[i]
print(f'Total number of notes = {number_of_notes}')
xxxxxxxxxx
def no_of_notes(amt, note):
n = [0, 0, 0]
min_amt = 0
for i in note:
min_amt += i
if amt < min_amt:
i = 0
while amt != 0:
n1 = amt // note[i]
amt = amt - (n1 * note[i])
n[i] += n1
i += 1
else:
n = [1, 1, 1]
if amt == min_amt:
return n
i = 0
amt -= min_amt
while amt != 0:
n1 = amt // note[i]
amt = amt - (n1 * note[i])
n[i] += n1
i += 1
return n
Amount = int(input('Enter a price: '))
note = [500, 200, 100]
result = no_of_notes(Amount, note)
number_of_notes = 0
for i in range(len(note)):
if result[i] != 0:
print(f' Rs.{note[i]} notes = {result[i]}')
number_of_notes += result[i]
print(f'Total number of notes = {number_of_notes}')
xxxxxxxxxx
import time
def insertionSort(arr):
global counter
counter = 0
for i in range(1, len(arr)):
key = arr[i]
j = i-1
counter = counter + 1
while j >= 0 and key < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j + 1] = key
repeat = 10
maxList = 20000
size = 2000
n = 0
counter = 0
while (n <= maxList):
d = [0] * (n)
sum = 0
k = 0
while (k < repeat):
i = n -1
while (i > 0):
d[i] = i
i -= 1
start_time = time.time()
insertionSort(d)
sum = sum + time.time() - start_time
k += 1
print("%d \t %0.2f ms \t %f ms" % (n, sum, sum/repeat))
n += size
xxxxxxxxxx
import string
# A list containing all characters
all_letters= string.ascii_letters
"""
create a dictionary to store the substitution
for the given alphabet in the plain text
based on the key
"""
dict1 = {}
key = 4
for i in range(len(all_letters)):
dict1[all_letters[i]] = all_letters[(i+key)%len(all_letters)]
plain_txt= "I am studying Data Encryption"
cipher_txt=[]
# loop to generate ciphertext
for char in plain_txt:
if char in all_letters:
temp = dict1[char]
cipher_txt.append(temp)
else:
temp =char
cipher_txt.append(temp)
cipher_txt= "".join(cipher_txt)
print("Cipher Text is: ",cipher_txt)
"""
create a dictionary to store the substitution
for the given alphabet in the cipher
text based on the key
"""
dict2 = {}
for i in range(len(all_letters)):
dict2[all_letters[i]] = all_letters[(i-key)%(len(all_letters))]
# loop to recover plain text
decrypt_txt = []
for char in cipher_txt:
if char in all_letters:
temp = dict2[char]
decrypt_txt.append(temp)
else:
temp = char
decrypt_txt.append(temp)
decrypt_txt = "".join(decrypt_txt)
print("Recovered plain text :", decrypt_txt)