diff options
| author | Simeon Simeonov | 2014-05-07 22:28:48 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2014-05-07 22:28:48 +0200 |
| commit | 60f47b04174d016cc86c6e06fdaf0794c3d14216 (patch) | |
| tree | b8137a37b8e4b81202db5943b509652c7f9564b5 /beinc_generic_client.py | |
| parent | 833ddeb822493f6ab2abd6f193b4c0bc8ea76854 (diff) | |
Git server / generic client and weechat client completed
Diffstat (limited to 'beinc_generic_client.py')
| -rwxr-xr-x | beinc_generic_client.py | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/beinc_generic_client.py b/beinc_generic_client.py new file mode 100755 index 0000000..6cffc84 --- /dev/null +++ b/beinc_generic_client.py | |||
| @@ -0,0 +1,158 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- coding: utf-8 -*- | ||
| 3 | |||
| 4 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v1.0 | ||
| 5 | # Copyright (C) 2013-2014 Simeon Simeonov | ||
| 6 | |||
| 7 | # This program is free software: you can redistribute it and/or modify | ||
| 8 | # it under the terms of the GNU General Public License as published by | ||
| 9 | # the Free Software Foundation, either version 3 of the License, or | ||
| 10 | # (at your option) any later version. | ||
| 11 | |||
| 12 | # This program is distributed in the hope that it will be useful, | ||
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | # GNU General Public License for more details. | ||
| 16 | |||
| 17 | # You should have received a copy of the GNU General Public License | ||
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | |||
| 21 | import argparse | ||
| 22 | import errno | ||
| 23 | import getpass | ||
| 24 | import httplib | ||
| 25 | import socket | ||
| 26 | import ssl | ||
| 27 | import sys | ||
| 28 | import urllib | ||
| 29 | import urllib2 | ||
| 30 | |||
| 31 | |||
| 32 | __author__ = 'Simeon Simeonov' | ||
| 33 | __version__ = '1.0' | ||
| 34 | __license__ = 'GPL3' | ||
| 35 | |||
| 36 | |||
| 37 | class ValidHTTPSConnection(httplib.HTTPConnection): | ||
| 38 | """ | ||
| 39 | Implements a simple CERT verification functionality | ||
| 40 | """ | ||
| 41 | |||
| 42 | default_port = httplib.HTTPS_PORT | ||
| 43 | |||
| 44 | def __init__(self, *args, **kwargs): | ||
| 45 | httplib.HTTPConnection.__init__(self, *args, **kwargs) | ||
| 46 | |||
| 47 | def connect(self): | ||
| 48 | sock = socket.create_connection((self.host, self.port), | ||
| 49 | self.timeout, self.source_address) | ||
| 50 | if self._tunnel_host: | ||
| 51 | self.sock = sock | ||
| 52 | self._tunnel() | ||
| 53 | self.sock = ssl.wrap_socket(sock, | ||
| 54 | ca_certs=global_beinc_cert_file, | ||
| 55 | cert_reqs=ssl.CERT_REQUIRED) | ||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | class ValidHTTPSHandler(urllib2.HTTPSHandler): | ||
| 60 | """ | ||
| 61 | Implements a simple CERT verification functionality | ||
| 62 | """ | ||
| 63 | |||
| 64 | def https_open(self, req): | ||
| 65 | return self.do_open(ValidHTTPSConnection, req) | ||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | def action_push(args): | ||
| 70 | """ | ||
| 71 | """ | ||
| 72 | try: | ||
| 73 | post_values = {'title': args.title, | ||
| 74 | 'message': args.message, | ||
| 75 | 'password': args.password} | ||
| 76 | data = urllib.urlencode(post_values) | ||
| 77 | req = urllib2.Request(args.url, data) | ||
| 78 | if args.cert: # check for cert validity | ||
| 79 | global global_beinc_cert_file # ugly hack | ||
| 80 | global_beinc_cert_file = args.cert | ||
| 81 | opener = urllib2.build_opener(ValidHTTPSHandler) | ||
| 82 | response = opener.open(req) | ||
| 83 | else: # ... or don't | ||
| 84 | response = urllib2.urlopen(req) | ||
| 85 | res_code = response.code | ||
| 86 | if res_code == 200: | ||
| 87 | print('Server responded: OK') | ||
| 88 | else: | ||
| 89 | print('Server responded: {0}'.format(res_code)) | ||
| 90 | print('Body:\n{0}'.format(response.read())) | ||
| 91 | response.close() | ||
| 92 | except urllib2.HTTPError as e: | ||
| 93 | sys.stderr.write('BEINC-server error ({0} - {1})\n'.format(e.code, e.reason)) | ||
| 94 | except Exception as e: | ||
| 95 | sys.stderr.write('BEINC generic client error: {0}\n'.format(e)) | ||
| 96 | sys.exit(errno.EPERM) | ||
| 97 | |||
| 98 | |||
| 99 | def main(): | ||
| 100 | |||
| 101 | parser = argparse.ArgumentParser( | ||
| 102 | description='The following options are available') | ||
| 103 | |||
| 104 | parser.add_argument('url', | ||
| 105 | metavar='URL', | ||
| 106 | type=str, | ||
| 107 | #dest='url', | ||
| 108 | #required=True, | ||
| 109 | help='Destination URL') | ||
| 110 | |||
| 111 | parser.add_argument('-c', '--cert-file', | ||
| 112 | metavar='FILE', | ||
| 113 | type=str, | ||
| 114 | dest='cert', | ||
| 115 | default='', | ||
| 116 | help='BEINC CA-cert to check the server-cert against') | ||
| 117 | |||
| 118 | parser.add_argument('-m', '--message', | ||
| 119 | metavar='MESSAGE', | ||
| 120 | type=str, | ||
| 121 | dest='message', | ||
| 122 | default='BEINC message', | ||
| 123 | help='BEINC message') | ||
| 124 | |||
| 125 | parser.add_argument('-p', '--password', | ||
| 126 | metavar='PASSWORD', | ||
| 127 | type=str, | ||
| 128 | dest='password', | ||
| 129 | default='', | ||
| 130 | help='Password') | ||
| 131 | |||
| 132 | parser.add_argument('-t', '--title', | ||
| 133 | metavar='TITLE', | ||
| 134 | type=str, | ||
| 135 | dest='title', | ||
| 136 | default='BEINC title', | ||
| 137 | help='BEINC title') | ||
| 138 | |||
| 139 | parser.add_argument('-v', '--version', | ||
| 140 | action='version', | ||
| 141 | version='%(prog)s {0}'.format(__version__), | ||
| 142 | help='display program-version and exit') | ||
| 143 | |||
| 144 | args = parser.parse_args() | ||
| 145 | |||
| 146 | if not args.password: | ||
| 147 | try: | ||
| 148 | args.password = getpass.getpass() | ||
| 149 | except Exception as e: | ||
| 150 | sys.stderr.write('Prompt terminated\n') | ||
| 151 | sys.exit(errno.EACCES) | ||
| 152 | |||
| 153 | action_push(args) | ||
| 154 | sys.exit(0) | ||
| 155 | |||
| 156 | |||
| 157 | if __name__ == '__main__': | ||
| 158 | main() | ||
