A modest string writer

qotd.py 689B

12345678910111213141516171819202122232425262728
  1. # -*- coding: utf-8 -*-
  2. import socket
  3. import sardine
  4. class Plugin(sardine.BasePlugin):
  5. def _read_args(self):
  6. args = self.data.args[:]
  7. try:
  8. self.host = args.pop(0)
  9. self.port = args.pop(0) if args else 17
  10. except IndexError:
  11. raise sardine.SardinePluginUsageError("HOST [PORT]")
  12. self.port = int(self.port)
  13. def _fetch_qotd(self):
  14. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  15. s.connect((self.host, self.port))
  16. self.qotd = s.recv(self.data.maxlen).strip()
  17. s.close()
  18. def render_plain(self):
  19. self._read_args()
  20. self._fetch_qotd()
  21. return self.qotd