| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -109,28 +109,28 @@ def __init__(self, host='', user='', passwd='', acct=''): | |||
| 109 | 109 | if user: self.login(user, passwd, acct) | |
| 110 | 110 | ||
| 111 | 111 | def connect(self, host = '', port = 0): | |
| 112 | - '''Connect to host. Arguments are: | ||
| 113 | - - host: hostname to connect to (string, default previous host) | ||
| 114 | - - port: port to connect to (integer, default previous port)''' | ||
| 115 | - if host: self.host = host | ||
| 116 | - if port: self.port = port | ||
| 117 | - self.passiveserver = 0 | ||
| 118 | - for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): | ||
| 119 | - af, socktype, proto, canonname, sa = res | ||
| 120 | - try: | ||
| 121 | - self.sock = socket.socket(af, socktype, proto) | ||
| 122 | - self.sock.connect(sa) | ||
| 123 | - except socket.error, msg: | ||
| 124 | - self.sock.close() | ||
| 125 | - self.sock = None | ||
| 126 | - continue | ||
| 127 | - break | ||
| 128 | - if not self.sock: | ||
| 129 | - raise socket.error, msg | ||
| 130 | - self.af = af | ||
| 131 | - self.file = self.sock.makefile('rb') | ||
| 132 | - self.welcome = self.getresp() | ||
| 133 | - return self.welcome | ||
| 112 | + '''Connect to host. Arguments are: | ||
| 113 | + - host: hostname to connect to (string, default previous host) | ||
| 114 | + - port: port to connect to (integer, default previous port)''' | ||
| 115 | + if host: self.host = host | ||
| 116 | + if port: self.port = port | ||
| 117 | + self.passiveserver = 0 | ||
| 118 | + for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): | ||
| 119 | + af, socktype, proto, canonname, sa = res | ||
| 120 | + try: | ||
| 121 | + self.sock = socket.socket(af, socktype, proto) | ||
| 122 | + self.sock.connect(sa) | ||
| 123 | + except socket.error, msg: | ||
| 124 | + self.sock.close() | ||
| 125 | + self.sock = None | ||
| 126 | + continue | ||
| 127 | + break | ||
| 128 | + if not self.sock: | ||
| 129 | + raise socket.error, msg | ||
| 130 | + self.af = af | ||
| 131 | + self.file = self.sock.makefile('rb') | ||
| 132 | + self.welcome = self.getresp() | ||
| 133 | + return self.welcome | ||
| 134 | 134 | ||
| 135 | 135 | def getwelcome(self): | |
| 136 | 136 | '''Get the welcome message from the server. | |
@@ -256,47 +256,47 @@ def sendport(self, host, port): | |||
| 256 | 256 | return self.voidcmd(cmd) | |
| 257 | 257 | ||
| 258 | 258 | def sendeprt(self, host, port): | |
| 259 | - '''Send a EPRT command with the current host and the given port number.''' | ||
| 260 | - af = 0 | ||
| 261 | - if self.af == socket.AF_INET: | ||
| 262 | - af = 1 | ||
| 263 | - if self.af == socket.AF_INET6: | ||
| 264 | - af = 2 | ||
| 265 | - if af == 0: | ||
| 266 | - raise error_proto, 'unsupported address family' | ||
| 267 | - fields = ['', `af`, host, `port`, ''] | ||
| 268 | - cmd = 'EPRT ' + string.joinfields(fields, '|') | ||
| 269 | - return self.voidcmd(cmd) | ||
| 259 | + '''Send a EPRT command with the current host and the given port number.''' | ||
| 260 | + af = 0 | ||
| 261 | + if self.af == socket.AF_INET: | ||
| 262 | + af = 1 | ||
| 263 | + if self.af == socket.AF_INET6: | ||
| 264 | + af = 2 | ||
| 265 | + if af == 0: | ||
| 266 | + raise error_proto, 'unsupported address family' | ||
| 267 | + fields = ['', `af`, host, `port`, ''] | ||
| 268 | + cmd = 'EPRT ' + string.joinfields(fields, '|') | ||
| 269 | + return self.voidcmd(cmd) | ||
| 270 | 270 | ||
| 271 | 271 | def makeport(self): | |
| 272 | - '''Create a new socket and send a PORT command for it.''' | ||
| 273 | - for res in socket.getaddrinfo(None, 0, self.af, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): | ||
| 274 | - af, socktype, proto, canonname, sa = res | ||
| 275 | - try: | ||
| 276 | - sock = socket.socket(af, socktype, proto) | ||
| 277 | - sock.bind(sa) | ||
| 278 | - except socket.error, msg: | ||
| 279 | - sock.close() | ||
| 280 | - sock = None | ||
| 281 | - continue | ||
| 282 | - break | ||
| 283 | - if not sock: | ||
| 284 | - raise socket.error, msg | ||
| 285 | - sock.listen(1) | ||
| 286 | - port = sock.getsockname()[1] # Get proper port | ||
| 287 | - host = self.sock.getsockname()[0] # Get proper host | ||
| 288 | - if self.af == socket.AF_INET: | ||
| 289 | - resp = self.sendport(host, port) | ||
| 290 | - else: | ||
| 291 | - resp = self.sendeprt(host, port) | ||
| 292 | - return sock | ||
| 272 | + '''Create a new socket and send a PORT command for it.''' | ||
| 273 | + for res in socket.getaddrinfo(None, 0, self.af, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): | ||
| 274 | + af, socktype, proto, canonname, sa = res | ||
| 275 | + try: | ||
| 276 | + sock = socket.socket(af, socktype, proto) | ||
| 277 | + sock.bind(sa) | ||
| 278 | + except socket.error, msg: | ||
| 279 | + sock.close() | ||
| 280 | + sock = None | ||
| 281 | + continue | ||
| 282 | + break | ||
| 283 | + if not sock: | ||
| 284 | + raise socket.error, msg | ||
| 285 | + sock.listen(1) | ||
| 286 | + port = sock.getsockname()[1] # Get proper port | ||
| 287 | + host = self.sock.getsockname()[0] # Get proper host | ||
| 288 | + if self.af == socket.AF_INET: | ||
| 289 | + resp = self.sendport(host, port) | ||
| 290 | + else: | ||
| 291 | + resp = self.sendeprt(host, port) | ||
| 292 | + return sock | ||
| 293 | 293 | ||
| 294 | 294 | def makepasv(self): | |
| 295 | - if self.af == socket.AF_INET: | ||
| 296 | - host, port = parse227(self.sendcmd('PASV')) | ||
| 297 | - else: | ||
| 298 | - host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername()) | ||
| 299 | - return host, port | ||
| 295 | + if self.af == socket.AF_INET: | ||
| 296 | + host, port = parse227(self.sendcmd('PASV')) | ||
| 297 | + else: | ||
| 298 | + host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername()) | ||
| 299 | + return host, port | ||
| 300 | 300 | ||
| 301 | 301 | def ntransfercmd(self, cmd, rest=None): | |
| 302 | 302 | """Initiate a transfer over the data connection. | |
@@ -316,9 +316,9 @@ def ntransfercmd(self, cmd, rest=None): | |||
| 316 | 316 | size = None | |
| 317 | 317 | if self.passiveserver: | |
| 318 | 318 | host, port = self.makepasv() | |
| 319 | - af, socktype, proto, canon, sa = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)[0] | ||
| 320 | - conn = socket.socket(af, socktype, proto) | ||
| 321 | - conn.connect(sa) | ||
| 319 | + af, socktype, proto, canon, sa = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)[0] | ||
| 320 | + conn = socket.socket(af, socktype, proto) | ||
| 321 | + conn.connect(sa) | ||
| 322 | 322 | if rest is not None: | |
| 323 | 323 | self.sendcmd("REST %s" % rest) | |
| 324 | 324 | resp = self.sendcmd(cmd) | |
@@ -575,17 +575,17 @@ def parse229(resp, peer): | |||
| 575 | 575 | Return ('host.addr.as.numbers', port#) tuple.''' | |
| 576 | 576 | ||
| 577 | 577 | if resp[:3] <> '229': | |
| 578 | - raise error_reply, resp | ||
| 578 | + raise error_reply, resp | ||
| 579 | 579 | left = string.find(resp, '(') | |
| 580 | 580 | if left < 0: raise error_proto, resp | |
| 581 | 581 | right = string.find(resp, ')', left + 1) | |
| 582 | 582 | if right < 0: | |
| 583 | - raise error_proto, resp # should contain '(|||port|)' | ||
| 583 | + raise error_proto, resp # should contain '(|||port|)' | ||
| 584 | 584 | if resp[left + 1] <> resp[right - 1]: | |
| 585 | - raise error_proto, resp | ||
| 585 | + raise error_proto, resp | ||
| 586 | 586 | parts = string.split(resp[left + 1:right], resp[left+1]) | |
| 587 | 587 | if len(parts) <> 5: | |
| 588 | - raise error_proto, resp | ||
| 588 | + raise error_proto, resp | ||
| 589 | 589 | host = peer[0] | |
| 590 | 590 | port = string.atoi(parts[3]) | |
| 591 | 591 | return host, port | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -357,22 +357,22 @@ def set_debuglevel(self, level): | |||
| 357 | 357 | ||
| 358 | 358 | def connect(self): | |
| 359 | 359 | """Connect to the host and port specified in __init__.""" | |
| 360 | - for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): | ||
| 361 | - af, socktype, proto, canonname, sa = res | ||
| 362 | - try: | ||
| 363 | - self.sock = socket.socket(af, socktype, proto) | ||
| 364 | - if self.debuglevel > 0: | ||
| 365 | - print "connect: (%s, %s)" % (self.host, self.port) | ||
| 366 | - self.sock.connect(sa) | ||
| 367 | - except socket.error, msg: | ||
| 368 | - if self.debuglevel > 0: | ||
| 369 | - print 'connect fail:', (self.host, self.port) | ||
| 370 | - self.sock.close() | ||
| 371 | - self.sock = None | ||
| 372 | - continue | ||
| 373 | - break | ||
| 374 | - if not self.sock: | ||
| 375 | - raise socket.error, msg | ||
| 360 | + for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): | ||
| 361 | + af, socktype, proto, canonname, sa = res | ||
| 362 | + try: | ||
| 363 | + self.sock = socket.socket(af, socktype, proto) | ||
| 364 | + if self.debuglevel > 0: | ||
| 365 | + print "connect: (%s, %s)" % (self.host, self.port) | ||
| 366 | + self.sock.connect(sa) | ||
| 367 | + except socket.error, msg: | ||
| 368 | + if self.debuglevel > 0: | ||
| 369 | + print 'connect fail:', (self.host, self.port) | ||
| 370 | + self.sock.close() | ||
| 371 | + self.sock = None | ||
| 372 | + continue | ||
| 373 | + break | ||
| 374 | + if not self.sock: | ||
| 375 | + raise socket.error, msg | ||
| 376 | 376 | ||
| 377 | 377 | def close(self): | |
| 378 | 378 | """Close the connection to the HTTP server.""" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,23 +73,23 @@ class POP3: | |||
| 73 | 73 | ||
| 74 | 74 | ||
| 75 | 75 | def __init__(self, host, port = POP3_PORT): | |
| 76 | - self.host = host | ||
| 77 | - self.port = port | ||
| 78 | - for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): | ||
| 79 | - af, socktype, proto, canonname, sa = res | ||
| 80 | - try: | ||
| 81 | - self.sock = socket.socket(af, socktype, proto) | ||
| 82 | - self.sock.connect(sa) | ||
| 83 | - except socket.error, msg: | ||
| 84 | - self.sock.close() | ||
| 85 | - self.sock = None | ||
| 86 | - continue | ||
| 87 | - break | ||
| 88 | - if not self.sock: | ||
| 89 | - raise socket.error, msg | ||
| 90 | - self.file = self.sock.makefile('rb') | ||
| 91 | - self._debugging = 0 | ||
| 92 | - self.welcome = self._getresp() | ||
| 76 | + self.host = host | ||
| 77 | + self.port = port | ||
| 78 | + for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): | ||
| 79 | + af, socktype, proto, canonname, sa = res | ||
| 80 | + try: | ||
| 81 | + self.sock = socket.socket(af, socktype, proto) | ||
| 82 | + self.sock.connect(sa) | ||
| 83 | + except socket.error, msg: | ||
| 84 | + self.sock.close() | ||
| 85 | + self.sock = None | ||
| 86 | + continue | ||
| 87 | + break | ||
| 88 | + if not self.sock: | ||
| 89 | + raise socket.error, msg | ||
| 90 | + self.file = self.sock.makefile('rb') | ||
| 91 | + self._debugging = 0 | ||
| 92 | + self.welcome = self._getresp() | ||
| 93 | 93 | ||
| 94 | 94 | ||
| 95 | 95 | def _putline(self, line): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -208,29 +208,29 @@ def connect(self, host='localhost', port = 0): | |||
| 208 | 208 | specified during instantiation. | |
| 209 | 209 | ||
| 210 | 210 | """ | |
| 211 | - if not port and (host.find(':') == host.rfind(':')): | ||
| 211 | + if not port and (host.find(':') == host.rfind(':')): | ||
| 212 | 212 | i = host.rfind(':') | |
| 213 | 213 | if i >= 0: | |
| 214 | 214 | host, port = host[:i], host[i+1:] | |
| 215 | 215 | try: port = int(port) | |
| 216 | 216 | except ValueError: | |
| 217 | 217 | raise socket.error, "nonnumeric port" | |
| 218 | 218 | if not port: port = SMTP_PORT | |
| 219 | - if self.debuglevel > 0: print 'connect:', (host, port) | ||
| 220 | - for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): | ||
| 221 | - af, socktype, proto, canonname, sa = res | ||
| 222 | - try: | ||
| 223 | - self.sock = socket.socket(af, socktype, proto) | ||
| 224 | - if self.debuglevel > 0: print 'connect:', (host, port) | ||
| 225 | - self.sock.connect(sa) | ||
| 226 | - except socket.error, msg: | ||
| 227 | - if self.debuglevel > 0: print 'connect fail:', (host, port) | ||
| 228 | - self.sock.close() | ||
| 229 | - self.sock = None | ||
| 230 | - continue | ||
| 231 | - break | ||
| 232 | - if not self.sock: | ||
| 233 | - raise socket.error, msg | ||
| 219 | + if self.debuglevel > 0: print 'connect:', (host, port) | ||
| 220 | + for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): | ||
| 221 | + af, socktype, proto, canonname, sa = res | ||
| 222 | + try: | ||
| 223 | + self.sock = socket.socket(af, socktype, proto) | ||
| 224 | + if self.debuglevel > 0: print 'connect:', (host, port) | ||
| 225 | + self.sock.connect(sa) | ||
| 226 | + except socket.error, msg: | ||
| 227 | + if self.debuglevel > 0: print 'connect fail:', (host, port) | ||
| 228 | + self.sock.close() | ||
| 229 | + self.sock = None | ||
| 230 | + continue | ||
| 231 | + break | ||
| 232 | + if not self.sock: | ||
| 233 | + raise socket.error, msg | ||
| 234 | 234 | (code, msg) = self.getreply() | |
| 235 | 235 | if self.debuglevel > 0: print "connect:", msg | |
| 236 | 236 | return (code, msg) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -136,18 +136,18 @@ def open(self, host, port=0): | |||
| 136 | 136 | port = TELNET_PORT | |
| 137 | 137 | self.host = host | |
| 138 | 138 | self.port = port | |
| 139 | - for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): | ||
| 140 | - af, socktype, proto, canonname, sa = res | ||
| 141 | - try: | ||
| 142 | - self.sock = socket.socket(af, socktype, proto) | ||
| 143 | - self.sock.connect(sa) | ||
| 144 | - except socket.error, msg: | ||
| 145 | - self.sock.close() | ||
| 146 | - self.sock = None | ||
| 147 | - continue | ||
| 148 | - break | ||
| 139 | + for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): | ||
| 140 | + af, socktype, proto, canonname, sa = res | ||
| 141 | + try: | ||
| 142 | + self.sock = socket.socket(af, socktype, proto) | ||
| 143 | + self.sock.connect(sa) | ||
| 144 | + except socket.error, msg: | ||
| 145 | + self.sock.close() | ||
| 146 | + self.sock = None | ||
| 147 | + continue | ||
| 148 | + break | ||
| 149 | 149 | if not self.sock: | |
| 150 | - raise socket.error, msg | ||
| 150 | + raise socket.error, msg | ||
| 151 | 151 | ||
| 152 | 152 | def __del__(self): | |
| 153 | 153 | """Destructor -- close the connection.""" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments