Python Forum
How to save jpg from cgi url
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save jpg from cgi url
#1
I have an IP camera that will return a snaphot by accessing the URL ...http://user:[email protected]/cgi-bin/snapshot.cgi?channel=0

Ive tried several different ways to save to a jpeg but none seem to work. If I use a URL of an actual jpg, it works fine. What is the best method? urllib, requests??
Reply
#2
I would just use urllib, so it would be best if you told us what your code is. My first guess would be that you're not saving it in binary mode.
Reply
#3
(May-05-2018, 02:44 AM)micseydel Wrote: I would just use urllib, so it would be best if you told us what your code is. My first guess would be that you're not saving it in binary mode.
import time

def timestamp():
    t = time.localtime()
    stamp = '%d-%02d-%02d-%02d%02d%02d' % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
    return stamp

url = 'http://user:[email protected]/cgi-bin/snapshot.cgi?channel=0'
localfile = '/storage/' + timestamp + '.jpg'

import urllib
import urllib2

data = urllib2.urlopen(url)

with open(localfile, 'wb') as f:
	f.write(data)
Reply
#4
(May-05-2018, 12:11 AM)lanceomni Wrote: What is the best method? urllib, requests??
Requests is what you should use and also Python 3 preferably 3.6.
Now you use Python 2,as your code would break in Python 3.
Example untested,should work 2 and 3:
import requests
import time

def timestamp():
    t = time.localtime()
    stamp = '%d-%02d-%02d-%02d%02d%02d' % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
    return stamp

url = 'http://user:[email protected]/cgi-bin/snapshot.cgi?channel=0'
url_get = requests.get(url)
localfile = '/storage/{}.jpg'.format(timestamp)
with open(localfile, 'wb') as f:
    f.write(url_get.content)
Reply
#5
snippsat,

When I use this code, I get a zero byte file named "<function timestamp at 0x10f134de8>.jpg"
Reply
#6
>>> localfile = '/storage/{}.jpg'.format(timestamp)
>>> localfile
'/storage/<function timestamp at 0x07727930>.jpg'

>>> # Fix
>>> localfile = '/storage/{}.jpg'.format(timestamp())
>>> localfile
'/storage/2018-05-06-204427.jpg'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 509 Oct-23-2023, 10:16 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020