A Python UPnP Media Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.5 KiB

  1. #!/usr/bin/env python
  2. # Licensed under the MIT license
  3. # http://opensource.org/licenses/mit-license.php
  4. # (c) 2005, Tim Potter <tpot@samba.org>
  5. import random
  6. import string
  7. import sys
  8. from twisted.python import log
  9. from twisted.internet import reactor
  10. def generateuuid():
  11. if False:
  12. return 'asdflkjewoifjslkdfj'
  13. return ''.join(map(lambda x: random.choice(string.letters), xrange(20)))
  14. listenAddr = sys.argv[1]
  15. if len(sys.argv) > 2:
  16. listenPort = int(sys.argv[2])
  17. if listenPort < 1024 or listenPort > 65535:
  18. raise ValueError, 'port out of range'
  19. else:
  20. listenPort = 8080
  21. log.startLogging(sys.stdout)
  22. # Create SSDP server
  23. from SSDP import SSDPServer, SSDP_PORT, SSDP_ADDR
  24. s = SSDPServer()
  25. port = reactor.listenMulticast(SSDP_PORT, s)
  26. port.joinGroup(SSDP_ADDR)
  27. port.setLoopbackMode(0) # don't get our own sends
  28. uuid = 'uuid:' + generateuuid()
  29. # Create SOAP server
  30. from twisted.web import server, resource, static
  31. from ContentDirectory import ContentDirectoryServer
  32. from ConnectionManager import ConnectionManagerServer
  33. class WebServer(resource.Resource):
  34. def __init__(self):
  35. resource.Resource.__init__(self)
  36. class RootDevice(static.File):
  37. def __init__(self):
  38. static.File.__init__(self, 'root-device.xml', defaultType = 'text/xml')
  39. root = WebServer()
  40. root.putChild('ContentDirectory', ContentDirectoryServer())
  41. root.putChild('ConnectionManager', ConnectionManagerServer())
  42. root.putChild('root-device.xml', RootDevice())
  43. # Area of server to serve media files from
  44. from MediaServer import MediaServer
  45. root.putChild('media', static.File('media'))
  46. site = server.Site(root)
  47. reactor.listenTCP(listenPort, site)
  48. # we need to do this after the children are there, since we send notifies
  49. s.register('%s::upnp:rootdevice' % uuid,
  50. 'upnp:rootdevice',
  51. 'http://%s:%d/root-device.xml' % (listenAddr, listenPort))
  52. s.register(uuid,
  53. uuid,
  54. 'http://%s:%d/root-device.xml' % (listenAddr, listenPort))
  55. s.register('%s::urn:schemas-upnp-org:device:MediaServer:1' % uuid,
  56. 'urn:schemas-upnp-org:device:MediaServer:1',
  57. 'http://%s:%d/root-device.xml' % (listenAddr, listenPort))
  58. s.register('%s::urn:schemas-upnp-org:service:ConnectionManager:1' % uuid,
  59. 'urn:schemas-upnp-org:device:ConnectionManager:1',
  60. 'http://%s:%d/root-device.xml' % (listenAddr, listenPort))
  61. s.register('%s::urn:schemas-upnp-org:service:ContentDirectory:1' % uuid,
  62. 'urn:schemas-upnp-org:device:ContentDirectory:1',
  63. 'http://%s:%d/root-device.xml' % (listenAddr, listenPort))
  64. # Main loop
  65. reactor.run()