equal
deleted
inserted
replaced
123 flags |= socket.NI_NUMERICSERV |
123 flags |= socket.NI_NUMERICSERV |
124 |
124 |
125 return socket.getnameinfo(sockaddr, flags) |
125 return socket.getnameinfo(sockaddr, flags) |
126 |
126 |
127 def socket_str (sock) : |
127 def socket_str (sock) : |
128 peer = sock.getpeername() |
128 # get connected peer |
|
129 try : |
|
130 peer = sock.getpeername() |
|
131 |
|
132 except socket.error as ex : |
|
133 # fails if socket is not connected XXX: even after EOF on read..? |
|
134 return str(ex) |
129 |
135 |
130 # lookup scheme |
136 # lookup scheme |
131 for scheme, (family, socktype) in URL : |
137 for scheme, (family, socktype) in URL : |
132 if family and family != sock.family : |
138 if family and family != sock.family : |
133 continue |
139 continue |
143 if scheme : |
149 if scheme : |
144 return "{scheme}://{host}:{port}".format(scheme=scheme, host=host, port=port) |
150 return "{scheme}://{host}:{port}".format(scheme=scheme, host=host, port=port) |
145 else : |
151 else : |
146 return "{host}:{port}".format(host=host, port=port) |
152 return "{host}:{port}".format(host=host, port=port) |
147 |
153 |
148 # XXX: SIGPIPE |
|
149 def nonblocking (call, *args, **kwargs) : |
154 def nonblocking (call, *args, **kwargs) : |
150 """ |
155 """ |
151 Call the given function, which read/writes on a nonblocking file, and return None if it would have blocked. |
156 Call the given function, which read/writes on a nonblocking file, and return None if it would have blocked. |
|
157 |
|
158 Raises EOFError on SIGPIPE/EPIPE. |
|
159 |
|
160 # XXX: does python handle SIGPIPE for us? |
152 """ |
161 """ |
153 |
162 |
154 try : |
163 try : |
155 return call(*args, **kwargs) |
164 return call(*args, **kwargs) |
156 |
165 |
157 except socket.error as ex : |
166 except socket.error as ex : |
158 # block? |
167 # block? |
159 if ex.errno == errno.EAGAIN or ex.errno == errno.EWOULDBLOCK: |
168 if ex.errno == errno.EAGAIN or ex.errno == errno.EWOULDBLOCK: |
160 # empty |
169 # empty |
161 return None |
170 return None |
|
171 |
|
172 elif ex.errno == errno.EPIPE : |
|
173 # XXX: write-eof? |
|
174 raise EOFError() |
162 |
175 |
163 else : |
176 else : |
164 raise |
177 raise |
165 |
178 |
166 class ReadStream (object) : |
179 class ReadStream (object) : |