I'm trying to convert some php code into python and am using curl. I've gotten most of it to be accepted, but when it gets to the result = pycurl.exec(Moe) it keeps throwing a syntax error. I guess that I'm not filling out the exec field correctly, but I can't seem to figure out where it is going wrong.
from urllib2 import urlopen
from ClientForm import ParseResponse
import cgi, cgitb
import webbrowser
import curl, pycurl
Moe = pycurl.Curl(wsdl)
Moe.setopt(pycurl.POST, 1)
Moe.setopt(pycurl.HTTPHEADER, ["Content-Type: text/xml"])
Moe.setopt(pycurl.HTTPAUTH, pycurl.BASIC)
Moe.setopt(pycurl.USERPWD, "userid:password")
Moe.setopt(pycurl.POSTFIELDS, Larry)
Moe.setopt(pycurl.SSL_VERIFYPEER, 0)
Moe.setopt(pycurl.SSLCERT, pemlocation)
Moe.setopt(pycurl.SSLKEY, keylocation)
Moe.setopt(pycurl.SSLKEYPASSWD, keypassword)
Moe.setopt(pycurl.RETURNTRANSFER, 1)
result = pycurl.exec(Moe)
pycurl.close(Moe)
Use result = Moe.perform() to execute your request.
PS:
Moe.setopt(pycurl.POSTFIELDS, Larry)
Is Larry actually a variable? If it's a string, quote it.
exec is a reserved word in Python, you cannot have a function of that name. Try reading the pycurl documentation to see what function you should be calling.
I haven't used pycurl myself, but maybe you want to call Moe.perform()?
Related
I am trying to pull reports through a (shadowserver) API - doc here:
The python script is written by ShadowServer and I have tested it with the simple usage example they are providing:
$ ./call-api.py test/ping '{}'
{"pong":"2020-10-26 23:06:37"}
Thus far, everything works fine. I can submit additional calls and I get a response back.
However, when i try to pass a JSON parameter to the script (that is executed in command prompt), such as the example below:
os.system('''[some_directory]\\call-api.py reports/query '{ "help":true }' pretty''')
I get the following result:
JSON Exception: Expecting value: line 1 column 1 (char 0)
I believe that it is an issue with the quotes - single/double/triple.. but I've tried everything and it keeps returning the same result.
Any ideas?
Many thanks!
You might want to try subprocess to see if you get the same result, e.g. like this
import subprocess
subprocess.Popen([
"[some_directory]\\call-api.py",
"reports/query",
'{"help": true}',
"pretty",
])
or use json.dumps to not worry about any quoting
import subprocess
import json
subprocess.Popen([
"[some_directory]\\call-api.py",
"reports/query",
json.dumps({"help": True}),
"pretty",
])
SitiSchu came up with the json.dumps idea.
I have a URL in this format:
https://aLongStringWithNumbers:anotherLongStringWithNumbers#somewhere.com/admin/someAPICall.json
which looks like not something that Python's urllib2 can understand, keep getting errors when using that with urllib2.open:
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
Is this a case where the library is not capable of interpreting that URL? obviously it works in the browser... Using Python 2.7
Any advice would be appreciated!
Thanks
- V
Python is interpreting the : as the port number in the URL. You need to force it to pull in as a url using something like quote
See quote from the non-accepted answer here
>>> import urllib2
>>> urllib2.quote('https://aLongStringWithNumbers:anotherLongStringWithNumbers#somewhere.com/admin/someAPICall.json')
I am trying to write a Python script which will use openssl command to output contents of few digital certificates. The problem is that I am not able to loop through the files with the subprocess.check_output([]) function. Here's what I have got so far:-
#!/usr/bin/env python3
import subprocess
import os
import glob
for f in glob.glob("*.cer"):
OUT_PUT = subprocess.check_output(['openssl','x509','-in','f','-noout','-text'])
print(type(OUT_PUT))
print(OUT_PUT.decode('utf-8'))
I get a feeling that something's wrong with the way I am placing the "f" variable in the function. The above code does not work.
Please advise.
This may or may not be your problem, but as it stands, you have f in quotes, so you're just passing through a string ("f") rather than the name of the file you wish to pass to openssl.
Am trying to test out urllib2. Here's my code:
import urllib2
response = urllib2.urlopen('http://pythonforbeginners.com/')
print response.info()
html = response.read()
response.close()
When I run it, I get:
Syntax Error: invalid syntax. Carrot points to line 3 (the print line). Any idea what's going on here? I'm just trying to follow a tutorial and this is the first thing they do...
Thanks,
Mariogs
In Python3 print is a function. Therefore it needs parentheses around its argument:
print(response.info())
In Python2, print is a statement, and hence does not require parentheses.
After correcting the SyntaxError, as alecxe points out, you'll probably encounter an ImportError next. That is because the Python2 module called urllib2 was renamed to urllib.request in Python3. So you'll need to change it to
import urllib.request as request
response = request.urlopen('http://pythonforbeginners.com/')
As you can see, the tutorial you are reading is meant for Python2. You might want to find a Python3 tutorial or Python3 urllib HOWTO to avoid running into more of these problems.
I am going around in circles and tried so many different ways so I guess my core understanding is wrong. I would be grateful for help in understanding my encoding/decoding issues.
import urllib2
result = urllib2.urlopen("https://graph.facebook.com/163146530455639")
rawdata = result.read().decode('utf-8')
print "HEADER: " + str(result.info())
print "I want this to work ", rawdata.find('http://www.facebook.com')
print "I dont want this to work ", rawdata.find('http:\/\/www.facebook.com')
I guess what im getting isnt utf-8 even though the header seems to say it is. Or as a newbie to Python im doing something dumb. :(
Thanks for any help,
Phil
You're getting JSON back from Facebook, so the easiest thing to do is use the built in json module to decode it (provided you're using Python 2.6+, otherwise you'll have to install).
import json
import urllib2
result = urllib2.urlopen("https://graph.facebook.com/163146530455639")
rawdata = result.read()
jsondata = json.load(rawdata)
print jsondata['link']
gives you:
u'http://www.facebook.com/GrosvenorCafe'