Submitting a file in Python

164 views
Skip to first unread message

Alexis T

unread,
Jun 12, 2020, 1:52:41 PM6/12/20
to astrometry
Hello,

I am a student trying to implement your API for astrometry.net into my own code. I've taken a part of the client.py code and I am trying to submit a file within this method to the server. I've been able to successfully logon and I've been able to successfully pull files from the server, but I haven't been able to submit files to the server. I have included my code below, and the error I am currently getting is commented on the line that pulls the error. I am new to JSON API, so I would appreciate some help in making this work. 

def getWCS(key):
logon = requests.post('http://nova.astrometry.net/api/login', data={'request-json': json.dumps({"apikey": key})})
session = logon.json()['session'] # Session number

#MODIFIED CLIENT.PY CODE
import random
boundary_key = ''.join([random.choice('0123456789') for i in range(19)])
boundary = '===============%s==' % boundary_key
headers = {'Content-Type':
'multipart/form-data; boundary="%s"' % boundary}
data_pre = (
'--' + boundary + '\n' +
'Content-Type: text/plain\r\n' +
'MIME-Version: 1.0\r\n' + # TypeError: can only concatenate str (not "module") to str
'Content-disposition: form-data; name="request-json"\r\n' +
'\r\n' +
json + '\n' +
'--' + boundary + '\n' +
'Content-Type: application/octet-stream\r\n' +
'MIME-Version: 1.0\r\n' +
'Content-disposition: form-data; name="file"; filename="files/DW Cnc V-20200215at080749_-25-1X1-300-V.fts"\r\n\r\n')
data_post = (
'\n' + '--' + boundary + '--\n')
data = data_pre.encode() + data_post.encode()

request = requests.get(url='http://nova.astrometry.net/api/upload', headers=headers, data=data)


print(request.json())
subid = request.json()['subid']
joburl = 'http://nova.astrometry.net/new_fits_file/' + subid
#print(joburl)
results = requests.get(joburl, allow_redirects=True)
#print(results.headers.get('content-type'))
open('file.fits', 'wb').write(results.content)

Dustin Lang

unread,
Jun 12, 2020, 2:31:37 PM6/12/20
to Alexis T, astrometry
You haven't defined 'json' for the image submission, so "json" here is referring to the JSON MODULE.

"
json + '\n' +
'--' + boundary + '\n' +
"

--
You received this message because you are subscribed to the Google Groups "astrometry" group.
To unsubscribe from this group and stop receiving emails from it, send an email to astrometry+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/astrometry/c66e9fa3-f810-417b-af29-421246b1346co%40googlegroups.com.

Alexis T

unread,
Jun 12, 2020, 2:44:10 PM6/12/20
to astrometry
Dustin,

Thank you! That was why I was getting that particular error. I've fixed the error by adding more code from the client, however I'm still getting a submission error when I try to submit the file. Updated code and error below: 

def getWCS(key):
logon = requests.post('http://nova.astrometry.net/api/login', data={'request-json': json.dumps({"apikey": key})})
session = logon.json()['session'] # Session number

#MODIFIED CLIENT.PY CODE
    python2json = json.dumps
args = {}
if session is not None:
args.update({'session': session})
print('Python:', args)
j = python2json(args)
print('Sending json:', j)
url = 'http://nova.astrometry.net/api/upload'
print('Sending to URL:', url)


import random
boundary_key = ''.join([random.choice('0123456789') for i in range(19)])
boundary = '===============%s==' % boundary_key
headers = {'Content-Type':
'multipart/form-data; boundary="%s"' % boundary}
data_pre = (
'--' + boundary + '\n' +
'Content-Type: text/plain\r\n' +
'MIME-Version: 1.0\r\n' +
            'Content-disposition: form-data; name="request-json"\r\n' +
'\r\n' +
            j + '\n' +

'--' + boundary + '\n' +
'Content-Type: application/octet-stream\r\n' +
'MIME-Version: 1.0\r\n' +
'Content-disposition: form-data; name="file"; filename="files/DW Cnc V-20200215at080749_-25-1X1-300-V.fts"\r\n\r\n')
data_post = (
'\n' + '--' + boundary + '--\n')
data = data_pre.encode() + data_post.encode()

    request = requests.get(url=url, headers=headers, data=data)


print(request.json()) #Prints "{'status': 'error', 'errormessage': 'no json'}
subid = request.json()['subid'] #KeyError: 'subid'
    joburl = 'http://nova.astrometry.net/new_fits_file/' + subid
#print(joburl)
results = requests.get(joburl, allow_redirects=True)
#print(results.headers.get('content-type'))
open('file.fits', 'wb').write(results.content)
To unsubscribe from this group and stop receiving emails from it, send an email to astro...@googlegroups.com.

Dustin Lang

unread,
Jun 12, 2020, 2:54:48 PM6/12/20
to Alexis T, astrometry
You want to read this

If you're using "requests", you should not be formatting your own multipart string.

But I'm not sure why you don't just import the client module, create a Client object and use that, rather than frankensteining the code.

--dstn


To unsubscribe from this group and stop receiving emails from it, send an email to astrometry+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/astrometry/017fc0e4-d5e9-4a32-9f21-ee9b9e674008o%40googlegroups.com.

Alexis T

unread,
Jun 12, 2020, 3:05:49 PM6/12/20
to astrometry
I had been following along with the API documentation here: http://astrometry.net/doc/net/api.html and I was trying to make it work like that, but I will probably swap over to using a client object, thank you. 

Dustin Lang

unread,
Jun 12, 2020, 3:09:42 PM6/12/20
to Alexis T, astrometry
yeah, those docs suck, sorry


To unsubscribe from this group and stop receiving emails from it, send an email to astrometry+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/astrometry/df75b61f-2539-4156-a462-9bdab29b7b4fo%40googlegroups.com.

Alexis T

unread,
Jun 16, 2020, 3:58:00 PM6/16/20
to astrometry
Just to update: the client.py file worked much better, I've got it down now. Thanks for the help!
Reply all
Reply to author
Forward
0 new messages