1: import time
2: import httplib
3: import base64
4: import StringIO
5: import pygame
6: from pygame.locals import *
7:
8: class trendnet:
9:
10: def __init__(self, ip, username='admin', password='admin'):
11:
12: self.ip = ip
13: self.username = username
14: self.password = password
15: self.base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
16:
17: def connect(self):
18:
19: h = httplib.HTTP(self.ip)
20: h.putrequest('GET','/cgi/mjpg/mjpeg.cgi')
21: h.putheader('Authorization', 'Basic %s' % self.base64string)
22: h.endheaders()
23: errcode, errmsg, headers = h.getreply()
24: self.file = h.getfile()
25:
26: def update(self, window, size, offset):
27:
28: data = self.file.readline()
29: if data[0:15] == 'Content-Length:':
30: count = int(data[16:])
31: s = self.file.read(count)
32: while s[0] != chr(0xff):
33: s = s[1:]
34:
35: p = StringIO.StringIO(s)
36:
37: try:
38: campanel = pygame.image.load(p).convert()
39: campanel = pygame.transform.scale(campanel, size)
40: window.blit(campanel, offset)
41:
42: except Exception, x:
43: print x
44:
45: p.close()
46:
47: if __name__ == '__main__':
48:
49: pygame.init()
50: screen = pygame.display.set_mode((660,500), 0, 32)
51:
52: pygame.display.set_caption('trendnet.py')
53:
54: background = pygame.Surface((660,500))
55: background.fill(pygame.Color('#E8E8E8'))
56: screen.blit(background, (0,0))
57:
58: camera = trendnet('192.168.1.103', 'admin', 'admin')
59: camera.connect()
60:
61: while True:
62:
63: camera.update(screen, (640,480), (10,10))
64: pygame.display.update()
65:
66: for event in pygame.event.get():
67: if event.type == QUIT:
68: sys.exit(0)
69:
70: time.sleep(.01)
download code