transcrypt connectionpool error - python

I am very new to coding and web development. I am a Systems Engineer and looking to get into the Web Development side of things. I took some python tutorials and pieced together a (probably very) rough python application. I would now like to take that application and put it on a website I have created so that I can allow others in the office to use the utility as well.
To that end, I installed transcrypt with the goal of converting the python code to javascript. When running transcrypt I get the following output:
Error while compiling (offending file last):
File 'c:/Scripting/Transcrypt/Meraki.py', line 1, at import of:
File 'c:/users/dab404/appdata/local/programs/python/python36/lib/site-packages/requests/init.py', line 43, at import of:
File 'c:/users/dab404/appdata/local/programs/python/python36/lib/site-packages/urllib3/init.py', line 8, namely:
Attempt to import module: connectionpool
Can't find any of:
c:/Scripting/Transcrypt/connectionpool.py
c:/Scripting/Transcrypt/javascript/connectionpool.mod.js
The error goes on to list about 10 other files that it needs to run. I am not sure how to fix this issue and would appreciate any help anyone can give me.
Here is my code:
import requests
import json
from meraki import meraki
base_url = "https://dashboard.meraki.com/api/v0/"
def List_Orgs(apikey): #A FUNCTION FOR LISTING ORGANIZATION ADMINS
myOrgs = meraki.myorgaccess(apikey)
for orgs in myOrgs:
print(orgs)
def List_Admins(URL_admin, headers):
x = requests.get(URL_admin, headers = headers)
myAdmins = x.json()
for admins in myAdmins:
print(admins)
def Add_Admin(URL, admin_data, headers): #FUNCTION FOR ADDING NEW ADMIN
TO AN ORGANIZATION
r = requests.request("POST", URL, data = admin_data, headers = headers)
print(r.status_code)
if (r.status_code) == 201:
print()
print()
print("Administrator successfully added!")
print()
else:
print()
print("Administrator was NOT successfully added. Please try again!")
print()
def Del_Admin(URL_del, headers): #FUNCTION FOR DELETING AN ADMIN FROM AN
ORGANIZATION
r = requests.request("DELETE", URL_del, headers = headers)
print(r.status_code)
if (r.status_code) == 204:
print()
print()
print("Administrator successfully deleted!")
print()
else:
print()
print("Administrator was NOT successfully deleted. Please try again!")
print()
apikey = input("What is your Meraki API key? ")
print()
print("******************************************")
print()
print("Here is a list of your Organizations. You will need the ID to answer
the next set of questions.")
print()
print()
List_Orgs(apikey)
print()
print()
headers = {
'X-Cisco-Meraki-API-Key': apikey,
'Content-Type': "application/json"
}
add_or_del = input("Would you like to add or delete an admin? ")
if add_or_del == ("add" or "Add" or "ADD"):
orgid = input("Which Organization would you like to add an admin to? ")
admin_name = input("What is the new Admin's First and Last name? ")
admin_email = input("What is " + admin_name + "'s email address? ")
admin_access = input("What level of access would you like " + admin_name +
" to have? (full or read-only) ")
admin_data = '{\n\t\"name\":\"' + admin_name + '\",\n\t\"email\":\"' +
admin_email + '\",\n\t\"orgAccess\":\"' + admin_access + '\"}'
URL = (base_url + 'organizations/' + orgid + '/admins')
Add_Admin(URL, admin_data, headers)
elif add_or_del == ("delete" or "Delete" or "DELETE"):
orgid = input("Which Organization would you like to delete an admin from?
")
URL_admin = (base_url + 'organizations/' + orgid + '/admins/')
print()
print("Here is a list of Admins in this Organization. You will need to
admin ID to answer the next question.")
print()
print()
List_Admins(URL_admin, headers)
print()
print()
adminid = input ("What is the admin's Meraki portal ID? ")
URL_del = (base_url + 'organizations/' + orgid + '/admins/' + adminid)
Del_Admin(URL_del, headers)
else:
print("Please type add or delete and try again.")'
Thanks!
David

The problem lies with the imports:
import requests
import json
from meraki import meraki
A module like requests is a standard Python module that isn't supported by Transcrypt, since it uses code written in C, that doesn't run in a browser.
For json there's a JavaScript counterpart that can be used directly from Transcrypt without problems.
Module meraki I don't know, so can't judge about.
Although a growing number of standard modules is supplied in the Transcrypt distribution, in general it makes use of JavaScript modules, since these are specially geared toward functionality that makes sense in a browser.
E.g. local file access is generally prohibited in a browser, so any module using that cannot do it's 'thing'.
See also:
http://www.transcrypt.org/docs/html/what_why.html#the-ecosystem-different-batteries
So in Transcrypt you program in Python, but the libs you use are mainly JavaScript. The exception are very common libs like math, cmath, random (partially), time, datetime, itertools, re etc.
To get an impression of how to use JavaScript libs from Transcrypt, take a look at:
http://www.transcrypt.org/examples
and also at:
http://www.transcrypt.org/docs/html/integration_javascript.html#mixed-examples
[EDIT]
I've taken another good look at your application, and I notice that it's a typical console application, using things like input and print. While these are supported in Transcrypt in a limited way, see
http://www.transcrypt.org/docs/html/integration_javascript.html#example-using-input-and-print-in-a-dom-terminal-element-in-your-browser
in general web applications work somewhat differently.
In general they are event driven, meaning that a number of GUI elements are pieced together, sometimes in HTML, sometimes in a script. These GUI elements then trigger events, that in turn trigger certain pieces of code (event handlers) to be run.
So a good next step may be to study this way of working. A nice, simple example in Transcrypt, of HTML/DOM and a script cooperating in this way is this one:
http://www.transcrypt.org/docs/html/installation_use.html#your-first-transcrypt-program
In many cases with a web application there's also interaction with a webserver, so part of the processing is done on the server.
You can e.g. use Bottle or Django for that, as is demoed at:
https://github.com/Michael-F-Ellis/NearlyPurePythonWebAppDemo

Related

Why is request module closing after giving an input?

I am working on a project that finds if a user is available or not on Instagram, (Basically that's only one of the functions.)
To do that I use this code from requests:
def start():
clear() # Clears the previous input through the OS module
print(inp + cut + "Stress them is booting...") # Inp + Cut are optional strings
use = input("Who do you want to attack? Enter here: >> ") # We take the input from here (Specifically the user)
response = requests.get("https://www.instagram.com/"+xuse+"/") # Check if the user exists, this does not work if you put # in the beginning.
if response.status_code == 404 :
PrintFatalError("User could not be found, try again.") # The user wasn't found
# Here the program stops and returns to the main program / input where I ask the user for a command.
elif response.status_code == 200 :
PrintFatalError("User " + use + " has been detected ! Proceed with the tool? ") # The user was found
f = input(" ") # This is the point
if f in "y":
print ("Beginning")
else:
print("Not a valid option, aborting.") # I abort since I don't want a loop in this phase.
Some other info that might help:
The program is fully CLI, I don't use a GUI.
This program uses while loops instead of for loops, for many reasons.
NOTE: PrintFatalError is a variable I created to display messages in red / green color. It still does not work with the classic print("string") way through.

How to call an API but only grab one specific piece of info in Python

I want to create a simple python program that calls the colornames.org API for the name of any given hex code inputted by the user. However, all I want my program to output is the "name" info.
How can I make it only output that and not all of the information?
Code below:
import requests
import json
hexcodeinput = input("Hex code you've found (format: FF0000, no #): ")
print(hexcodeinput + " is your selected hex code. Searching...")
response = requests.get("https://colornames.org/search/json/?hex=" + (hexcodeinput))
print(response.text)
You should get the response as json, not as plain text. Then it is a dict which you can use:
import requests
hex_code = input("Hex code you've found (format: FF0000, no #): ")
print("%s is your selected hex code. Searching..." % hex_code)
response = requests.get("https://colornames.org/search/json/?hex="+hex_code)
details = response.json() # a dict with all the info
print(details['name']) # get the name from that dict

Incomplete HAR list using Python: Browsermobproxy, selenium, phantomJS

Fairly new to python, I learn by doing, so I thought I'd give this project a shot. Trying to create a script which finds the google analytics request for a certain website parses the request payload and does something with it.
Here are the requirements:
Ask user for 2 urls ( for comparing the payloads from 2 diff. HAR payloads)
Use selenium to open the two urls, use browsermobproxy/phantomJS to
get all HAR
Store the HAR as a list
From the list of all HAR files, find the google analytics request, including the payload
If Google Analytics tag found, then do things....like parse the payload, etc. compare the payload, etc.
Issue: Sometimes for a website that I know has google analytics, i.e. nytimes.com - the HAR that I get is incomplete, i.e. my prog. will say "GA Not found" but that's only because the complete HAR was not captured so when the regex ran to find the matching HAR it wasn't there. This issue in intermittent and does not happen all the time. Any ideas?
I'm thinking that due to some dependency or latency, the script moved on and that the complete HAR didn't get captured. I tried the "wait for traffic to stop" but maybe I didn't do something right.
Also, as a bonus, I would appreciate any help you can provide on how to make this script run fast, its fairly slow. As I mentioned, I'm new to python so go easy :)
This is what I've got thus far.
import browsermobproxy as mob
from selenium import webdriver
import re
import sys
import urlparse
import time
from datetime import datetime
def cleanup():
s.stop()
driver.quit()
proxy_path = '/Users/bob/Downloads/browsermob-proxy-2.1.4-bin/browsermob-proxy-2.1.4/bin/browsermob-proxy'
s = mob.Server(proxy_path)
s.start()
proxy = s.create_proxy()
proxy_address = "--proxy=127.0.0.1:%s" % proxy.port
service_args = [proxy_address, '--ignore-ssl-errors=yes', '--ssl-protocol=any'] # so that i can do https connections
driver = webdriver.PhantomJS(executable_path='/Users/bob/Downloads/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/bin/phantomjs', service_args=service_args)
driver.set_window_size(1400, 1050)
urlLists = []
collectTags = []
gaCollect = 0
varList = []
for x in range(0,2): # I want to ask the user for 2 inputs
url = raw_input("Enter a website to find GA on: ")
time.sleep(2.0)
urlLists.append(url)
if not url:
print "You need to type something in...here"
sys.exit()
#gets the two user url and stores in list
for urlList in urlLists:
print urlList, 'start 2nd loop' #printing for debug purpose, no need for this
if not urlList:
print 'Your Url list is empty'
sys.exit()
proxy.new_har()
driver.get(urlList)
#proxy.wait_for_traffic_to_stop(15, 30) #<-- tried this but did not do anything
for ent in proxy.har['log']['entries']:
gaCollect = (ent['request']['url'])
print gaCollect
if re.search(r'google-analytics.com/r\b', gaCollect):
print 'Found GA'
collectTags.append(gaCollect)
time.sleep(2.0)
break
else:
print 'No GA Found - Ending Prog.'
cleanup()
sys.exit()
cleanup()
This might be a stale question, but I found an answer that worked for me.
You need to change two things:
1 - Remove sys.exit() -- this causes your programme to stop after the first iteration through the ent list, so if what you want is not the first thing, it won't be found
2 - call new_har with the captureContent option enabled to get the payload of requests:
proxy.new_har(options={'captureHeaders':True, 'captureContent': True})
See if that helps.

Module urllib.request not getting data

I am trying to test this demo program from lynda using Python 3. I am using Pycharm as my IDE. I already added and installed the request package, but when I run the program, it runs cleanly and shows a message "Process finished with exit code 0", but does not show any output from print statement. Where am I going wrong ?
import urllib.request # instead of urllib2 like in Python 2.7
import json
def printResults(data):
# Use the json module to load the string data into a dictionary
theJSON = json.loads(data)
# now we can access the contents of the JSON like any other Python object
if "title" in theJSON["metadata"]:
print(theJSON["metadata"]["title"])
# output the number of events, plus the magnitude and each event name
count = theJSON["metadata"]["count"];
print(str(count) + " events recorded")
# for each event, print the place where it occurred
for i in theJSON["features"]:
print(i["properties"]["place"])
# print the events that only have a magnitude greater than 4
for i in theJSON["features"]:
if i["properties"]["mag"] >= 4.0:
print("%2.1f" % i["properties"]["mag"], i["properties"]["place"])
# print only the events where at least 1 person reported feeling something
print("Events that were felt:")
for i in theJSON["features"]:
feltReports = i["properties"]["felt"]
if feltReports != None:
if feltReports > 0:
print("%2.1f" % i["properties"]["mag"], i["properties"]["place"], " reported " + str(feltReports) + " times")
# Open the URL and read the data
urlData = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"
webUrl = urllib.request.urlopen(urlData)
print(webUrl.getcode())
if webUrl.getcode() == 200:
data = webUrl.read()
data = data.decode("utf-8") # in Python 3.x we need to explicitly decode the response to a string
# print out our customized results
printResults(data)
else:
print("Received an error from server, cannot retrieve results " + str(webUrl.getcode()))
Not sure if you left this out on purpose, but this script isn't actually executing any code beyond the imports and function definition. Assuming you didn't leave it out on purpose, you would need the following at the end of your file.
if __name__ == '__main__':
data = "" # your data
printResults(data)
The check on __name__ equaling "__main__" is just so your code is only executing when the file is explicitly run. To always run your printResults(data) function when the file is accessed (like, say, if its imported into another module) you could just call it at the bottom of your file like so:
data = "" # your data
printResults(data)
I had to restart the IDE after installing the module. I just realized and tried it now with "Run as Admin". Strangely seems to work now.But not sure if it was a temp error, since even without restart, it was able to detect the module and its methods.
Your comments re: having to restart your IDE makes me think that pycharm might not automatically detect newly installed python packages. This SO answer seems to offer a solution.
SO answer

Python newbie - Input strings, return a value to a web page

I've got a program I would like to use to input a password and one or multiple strings from a web page. The program takes the strings and outputs them to a time-datestamped text file, but only if the password matches the set MD5 hash.
The problems I'm having here are that
I don't know how to get this code on the web. I have a server, but is it as easy as throwing pytext.py onto my server?
I don't know how to write a form for the input to this script and how to get the HTML to work with this program. If possible, it would be nice to make it a multi-line input box... but it's not necessary.
I want to return a value to a web page to let the user know if the password authenticated successfully or failed.
dtest
import sys
import time
import getopt
import hashlib
h = hashlib.new('md5')
var = sys.argv[1]
print "Password: ", var
h.update(var)
print h.hexdigest()
trial = h.hexdigest()
check = "86fe2288ac154c500983a8b89dbcf288"
if trial == check:
print "Password success"
time_stamp = time.strftime('%Y-%m-%d_%H-%M-%S', (time.localtime(time.time())))
strFile = "txt_" + str(time_stamp) + ".txt"
print "File created: txt_" + str(time_stamp) + ".txt"
#print 'The command line arguments are:'
#for i in sys.argv:
#print i
text_file = open(strFile, "w")
text_file.write(str(time_stamp) + "\n")
for i in range(2, len(sys.argv)):
text_file.write(sys.argv[i] + "\n")
#print 'Debug to file:', sys.argv[i]
text_file.close()
else:
print "Password failure"
You'll need to read up on mod_python (if you're using Apache) and the Python CGI module.
Take a look at django. It's an excellent web framework that can accomplish exactly what you are asking. It also has an authentication module that handles password hashing and logins for you.

Categories