I have a python script that I want to run in Redhat 6.7 OS but it is constantly failing.
**Python version: 2.7.13 (initially it had default version which I have symlink to usr/local/bin/python2.7, not sure if it has changed to 2.7 but when I type which is python in terminal it shows the location usr/local/bin/python.)
Script to be run on: OS = Redhat 6.7
Script written in: OS = Windows10 (python ver 2.7.11)
code:
import urllib
import json
url = 'https://username:pass#api.amsterdampc.com'# sample URL(tested on 'api.openweathermap.org/data/2.5/weather?q=London' too gives the same error)
data = json.load(urllib.urlopen(url)) #should return a json data
print data
Here print data is raising "json decoder error", when i looked back into the steps I found out urllib.urlopen(url) is not at all returning the required json data instead of some ml response/empty at times.
Is there any specific changes I need to do if I run a python script in different OS isn't python a platform independent language?
By and large, python is reasonably platform independant. But that doesn't mean that there aren't differences between platforms. If you look through the documentation for the standard library, you will find notes that some functions or classes are only available on certain platforms. And e.g. the way multiprocessing works is also different between UNIX-like operating systems and ms-windows.
In this case you mention that the trouble begins with the fact that urllib.urlopen doesn't return what you expect. This is probably not an issue with the Python code. I suspect it is a networking/routing/firewall issue. You would have to show the returned not-JSON data to be sure.
As an aside, if you want to use HTML in Python, do yourself a favour and use the requests module. It is a lot more user-friendly then urllib.
Edit 1:
It says:
Your request could not be processed. Request could not be handled
This could be caused by a misconfiguration, or possibly a malformed request.
So there are two possible causes:
misconfiguration
malformed request
The network object returned by urllib.urlopen() has some extra methods compared to files, like info() and getcode(). Using those might yield some extra information about why the request failed.
If you do a POST request, the information has to be formatted and encoded in a certain way. If you use requests.post, it will handle these details for you.
Related
Trying to download some protein data from PDB using Biopython's Bio.PDB.PDBList
Here is a min. reproducible example:
from Bio.PDB import PDBList
pdbl=PDBList()
pdbl.retrieve_pdb_file('1GAV', file_format="pdb")
This returns:
Downloading PDB structure '1GAV'...
Desired structure doesn't exists
Desired behavior is download of the PDB file to the working directory.
Possibly useful info:
Using python 3
Do not want to download whole PDB, just pick and choose files
Using a proxy, but I don't think that's the problem because Biopython uses urllib to make requests and I tried using urllib with my proxy settings and it worked fine.
I've tried for a few different PDB code/IDs and for other file types ("mmCif", "bundle") and it returns the same thing
No error is being hit, it just can't find the file in PDB apparently?
The folder where the file should appear does get made in the working directory, but the folder is empty
We think the problem has to do with our corporate VPN because it works when the VPN is off (although proxy still on).
So as sammam said, no problems in the code.
Don't know the specifics of why this occurs with our VPN, will update if I find out.
I'm getting the same error message, which I looked at the source code and found out that the "Desired structure doesn't exists" message outputs whenever an IOError is hit.
Actually, I am creating several utilities to deal with AWS CLI responses, which responds with json.
My whole scripts ecosystem are based in Python, and usually I create tiny utilities that fetches the json data and parses to readable/organized data.
But I have noticed that I am taking so much time to creates scripts, besides hiding the data structure responses, something that may also be useful.
A much more interesting alternative would create a script that just receives the json data from aws cli and with a map as argument, filters down the information to what I need. And I pretend do this by a simple shell redirect (pipe).
Right now, I can do something like that in Node.js script. Following illustrates exactly how I would like to operates in Python:
Unless someone says that does not have a way to redirect shell command output to a python script, I prefer not to maintain just this piece of code in a different language than all the others.
How do I do the shell output redirection to a python script?
You could read sys.stdin in Python. My understanding of the above javascript code is translates to this in Python:
import sys
import json
object_data = json.loads(sys.stdin.read())
print(object_data['description'])
However do note that the AWS API has a Python SDK, so you probably don't have to pipe around these JSON outputs at all.
I've tried pybitcoin already, but I get a ModuleError for "services". I've tried multiple scripts from Google but they are all for Python2.
I tried to use this to get started
import ecdsa
return ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1).to_string()
but it returns bytes not a string. I assume it's a version issue because it's GitHub only mentions 3.1 and 3.2.
I want the addresses to be completely random.
Is there a reasonably standard and cross platform way to print text (or even PS/PDF) to the system defined printer?
Assuming CPython here, not something clever like using Jython and the Java printing API.
This has only been tested on Windows:
You can do the following:
import os
os.startfile("C:/Users/TestFile.txt", "print")
This will start the file, in its default opener, with the verb 'print', which will print to your default printer.Only requires the os module which comes with the standard library
Unfortunately, there is no standard way to print using Python on all platforms. So you'll need to write your own wrapper function to print.
You need to detect the OS your program is running on, then:
For Linux -
import subprocess
lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE)
lpr.stdin.write(your_data_here)
For Windows: http://timgolden.me.uk/python/win32_how_do_i/print.html
More resources:
Print PDF document with python's win32print module?
How do I print to the OS's default printer in Python 3 (cross platform)?
To print to any printer on the network you can send a PJL/PCL print job directly to a network printer on port 9100.
Please have a look at the below link that should give a good start:
http://frank.zinepal.com/printing-directly-to-a-network-printer
Also, If there is a way to call Windows cmd you can use FTP put to print your page on 9100. Below link should give you details, I have used this method for HP printers but I believe it will work for other printers.
http://h20000.www2.hp.com/bizsupport/TechSupport/Document.jsp?objectID=bpj06165
You can try wx library. It's a cross platform UI library. Here you can find the printing tutorial:
https://web.archive.org/web/20160619163747/http://wiki.wxpython.org/Printing
I find this to be the superior solution, at least when dealing with web applications. The idea is this: convert the HTML page to a PDF document and send that to a printer via gsprint.
Even though gsprint is no longer in development, it works really, really well. You can choose the printer and the page orientation and size among several other options.
I convert the web page to PDF using Puppeteer, Chrome's headless browser. But you need to pass in the session cookie to maintain credentials.
Is there a reasonably standard and cross platform way to print text (or even PS/PDF) to the system defined printer?
Assuming CPython here, not something clever like using Jython and the Java printing API.
This has only been tested on Windows:
You can do the following:
import os
os.startfile("C:/Users/TestFile.txt", "print")
This will start the file, in its default opener, with the verb 'print', which will print to your default printer.Only requires the os module which comes with the standard library
Unfortunately, there is no standard way to print using Python on all platforms. So you'll need to write your own wrapper function to print.
You need to detect the OS your program is running on, then:
For Linux -
import subprocess
lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE)
lpr.stdin.write(your_data_here)
For Windows: http://timgolden.me.uk/python/win32_how_do_i/print.html
More resources:
Print PDF document with python's win32print module?
How do I print to the OS's default printer in Python 3 (cross platform)?
To print to any printer on the network you can send a PJL/PCL print job directly to a network printer on port 9100.
Please have a look at the below link that should give a good start:
http://frank.zinepal.com/printing-directly-to-a-network-printer
Also, If there is a way to call Windows cmd you can use FTP put to print your page on 9100. Below link should give you details, I have used this method for HP printers but I believe it will work for other printers.
http://h20000.www2.hp.com/bizsupport/TechSupport/Document.jsp?objectID=bpj06165
You can try wx library. It's a cross platform UI library. Here you can find the printing tutorial:
https://web.archive.org/web/20160619163747/http://wiki.wxpython.org/Printing
I find this to be the superior solution, at least when dealing with web applications. The idea is this: convert the HTML page to a PDF document and send that to a printer via gsprint.
Even though gsprint is no longer in development, it works really, really well. You can choose the printer and the page orientation and size among several other options.
I convert the web page to PDF using Puppeteer, Chrome's headless browser. But you need to pass in the session cookie to maintain credentials.