本文整理匯總了Python中gevent.socket.error方法的典型用法代碼示例。如果您正苦於以下問題:Python socket.error方法的具體用法?Python socket.error怎麽用?Python socket.error使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gevent.socket
的用法示例。
在下文中一共展示了socket.error方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: read
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def read(self, size):
while len(self.buffer) < size:
self.ensure_connection()
try:
packet = self.socket.recv(self.buffer_size)
except socket.error as error:
if error.errno in (EDEADLK, EAGAIN, EWOULDBLOCK):
gevent.sleep()
continue
six.raise_from(NSQSocketError(*error.args), error)
if not packet:
self.close()
self.buffer += packet
data = self.buffer[:size]
self.buffer = self.buffer[size:]
return data
示例2: upgrade_to_tls
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def upgrade_to_tls(
self,
keyfile=None,
certfile=None,
cert_reqs=CERT_NONE,
ca_certs=None,
ssl_version=PROTOCOL_TLSv1_2
):
self.ensure_connection()
try:
self.socket = SSLSocket(
self.socket,
keyfile=keyfile,
certfile=certfile,
cert_reqs=cert_reqs,
ca_certs=ca_certs,
ssl_version=ssl_version,
)
except socket.error as error:
six.raise_from(NSQSocketError(*error.args), error)
示例3: handle
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def handle(self, sock, addr):
logger.debug('Incomming connection %s:%s', *addr[:2])
if self.relay and not addr[0] in self.remoteaddr:
logger.debug('Not in remoteaddr', *addr[:2])
return
try:
with Timeout(self.timeout, ConnectionTimeout):
sc = SMTPChannel(self, sock, addr, self.data_size_limit)
while not sc.closed:
sc.handle_read()
except ConnectionTimeout:
logger.warn('%s:%s Timeouted', *addr[:2])
try:
sc.smtp_TIMEOUT()
except Exception as err:
logger.debug(err)
except Exception as err:
logger.error(err)
# API for "doing something useful with the message"
示例4: smtp_STARTTLS
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def smtp_STARTTLS(self, arg):
if arg:
self.push('501 Syntax: STARTTLS')
return
self.push('220 Ready to start TLS')
if self.data:
self.push('500 Too late to changed')
return
try:
self.conn = ssl.wrap_socket(self.conn, **self.server.ssl)
self.state = self.COMMAND
self.seen_greeting = 0
self.rcpttos = []
self.mailfrom = None
self.tls = True
except Exception as err:
logger.error(err, exc_info=True)
self.push('503 certificate is FAILED')
self.close_when_done()
示例5: connect
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def connect(self, server_addr):
self._new_socket()
logger.debug("Attempting connection to %s", str(server_addr))
try:
self._connect(server_addr)
except socket.error:
return False
self.server_addr = server_addr
self.recv_queue.queue.clear()
self._reader = gevent.spawn(self._reader_loop)
self._writer = gevent.spawn(self._writer_loop)
logger.debug("Connected.")
self.event_connected.set()
return True
示例6: _real_connect
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def _real_connect(self, addr, connect_ex):
if self.server_side:
raise ValueError("can't connect in server-side mode")
# Here we assume that the socket is client-side, and not
# connected at the time of the call. We connect it, then wrap it.
if self._connected:
raise ValueError("attempt to connect already-connected SSLSocket!")
self._sslobj = self.context._wrap_socket(self._sock, False, self.server_hostname)
try:
if connect_ex:
rc = socket.connect_ex(self, addr)
else:
rc = None
socket.connect(self, addr)
if not rc:
if self.do_handshake_on_connect:
self.do_handshake()
self._connected = True
return rc
except socket_error:
self._sslobj = None
raise
示例7: _real_connect
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def _real_connect(self, addr, connect_ex):
if self.server_side:
raise ValueError("can't connect in server-side mode")
# Here we assume that the socket is client-side, and not
# connected at the time of the call. We connect it, then wrap it.
if self._connected:
raise ValueError("attempt to connect already-connected SSLSocket!")
self._sslobj = self.context._wrap_socket(self._sock, False, self.server_hostname, ssl_sock=self)
try:
if connect_ex:
rc = socket.connect_ex(self, addr)
else:
rc = None
socket.connect(self, addr)
if not rc:
self._connected = True
if self.do_handshake_on_connect:
self.do_handshake()
return rc
except socket_error:
self._sslobj = None
raise
示例8: send_message_to_slave
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def send_message_to_slave(message, address):
try:
payload = generate_msgpack_message_payload(message)
except TypeError:
logger.exception('Failed encoding message %s as msgpack', message)
metrics.incr('rpc_message_pass_fail_cnt')
return False
pretty_address = '%s:%s' % address
message_id = message.get('message_id', '?')
try:
s = socket.create_connection(address)
s.send(payload)
sender_resp = msgpack_unpack_msg_from_socket(s)
s.close()
except socket.error:
logging.exception('Failed connecting to %s to send message (ID %s)',
pretty_address, message_id)
metrics.incr('rpc_message_pass_fail_cnt')
return False
if sender_resp == 'OK':
access_logger.info('Successfully passed message (ID %s) to %s for sending',
message_id, pretty_address)
metrics.incr('rpc_message_pass_success_cnt')
return True
else:
logger.error('Failed sending message (ID %s) through %s: %s',
message_id, pretty_address, sender_resp)
metrics.incr('rpc_message_pass_fail_cnt')
return False
示例9: handle_slave_send
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def handle_slave_send(socket, address, req):
message = req['data']
message_id = message.get('message_id', '?')
message['to_slave'] = True
try:
runtime = send_funcs['message_send_enqueue'](message)
response = 'OK'
access_logger.info('Message (ID %s) from master %s queued successfully', message_id, address)
except Exception:
response = 'FAIL'
logger.exception('Queueing message (ID %s) from master %s failed.')
access_logger.error('Failed queueing message (ID %s) from master %s: %s', message_id, address, runtime)
metrics.incr('slave_message_send_fail_cnt')
socket.sendall(msgpack.packb(response))
示例10: _real_connect
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def _real_connect(self, addr, connect_ex):
if self.server_side:
raise ValueError("can't connect in server-side mode")
# Here we assume that the socket is client-side, and not
# connected at the time of the call. We connect it, then wrap it.
if self._connected:
raise ValueError("attempt to connect already-connected SSLSocket!")
self._sslobj = self._context._wrap_socket(self._sock, False, self.server_hostname)
if self._session is not None: # 3.6
self._sslobj = SSLObject(self._sslobj, owner=self, session=self._session)
try:
if connect_ex:
rc = socket.connect_ex(self, addr)
else:
rc = None
socket.connect(self, addr)
if not rc:
if self.do_handshake_on_connect:
self.do_handshake()
self._connected = True
return rc
except socket_error:
self._sslobj = None
raise
示例11: update_environ
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def update_environ(self):
"""
Called before the first request is handled to fill in WSGI environment values.
This includes getting the correct server name and port.
"""
address = self.address
if isinstance(address, tuple):
if 'SERVER_NAME' not in self.environ:
try:
name = socket.getfqdn(address[0])
except socket.error:
name = str(address[0])
if PY3 and not isinstance(name, str):
name = name.decode('ascii') # python 2 pylint:disable=redefined-variable-type
self.environ['SERVER_NAME'] = name
self.environ.setdefault('SERVER_PORT', str(address[1]))
else:
self.environ.setdefault('SERVER_NAME', '')
self.environ.setdefault('SERVER_PORT', '')
示例12: _real_connect
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def _real_connect(self, addr, connect_ex):
if self.server_side:
raise ValueError("can't connect in server-side mode")
# Here we assume that the socket is client-side, and not
# connected at the time of the call. We connect it, then wrap it.
if self._connected:
raise ValueError("attempt to connect already-connected SSLSocket!")
self._sslobj = self._context._wrap_socket(self._sock, False, self.server_hostname, ssl_sock=self)
try:
if connect_ex:
rc = socket.connect_ex(self, addr)
else:
rc = None
socket.connect(self, addr)
if not rc:
self._connected = True
if self.do_handshake_on_connect:
self.do_handshake()
return rc
except socket_error:
self._sslobj = None
raise
示例13: update_environ
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def update_environ(self):
"""
Called before the first request is handled to fill in WSGI environment values.
This includes getting the correct server name and port.
"""
address = self.address
if isinstance(address, tuple):
if 'SERVER_NAME' not in self.environ:
try:
name = socket.getfqdn(address[0])
except socket.error:
name = str(address[0])
if PY3 and not isinstance(name, str):
name = name.decode('ascii')
self.environ['SERVER_NAME'] = name
self.environ.setdefault('SERVER_PORT', str(address[1]))
else:
self.environ.setdefault('SERVER_NAME', '')
self.environ.setdefault('SERVER_PORT', '')
示例14: get_ip_version
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def get_ip_version(ip):
# CR: http://stackoverflow.com/questions/11827961/checking-for-ip-addresses
try:
socket.inet_aton(ip)
return 4
except socket.error:
pass
try:
socket.inet_pton(socket.AF_INET6, ip)
return 6
except socket.error:
pass
raise ValueError(ip)
示例15: parse_config_file
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import error [as 別名]
def parse_config_file(config_file, config_spec_file):
assert validators.PanoptesValidators.valid_nonempty_string(config_file), u'config_file must be a non-empty str'
assert validators.PanoptesValidators.valid_nonempty_string(config_spec_file), \
u'config_spec_file must be a non empty str'
try:
config = ConfigObj(config_file, configspec=config_spec_file, interpolation=u'template', file_error=True)
except IOError as e:
raise PanoptesConfigurationParsingError(u'Error reading file: %s' % str(e))
except ConfigObjError as e:
raise PanoptesConfigurationParsingError(u'Error parsing config file "%s": %s' % (config_file, str(e)))
validator = Validator()
result = config.validate(validator, preserve_errors=True)
if result is not True:
errors = u''
for (section_list, key, error) in flatten_errors(config, result):
if key is None:
errors += u'Section(s) ' + u','.join(section_list) + u' are missing\n'
else:
errors += u'The "' + key + u'" key in section "' + u','.join(section_list) + u'" failed validation\n'
raise PanoptesConfigurationParsingError(u'Error parsing the configuration file: %s' % errors)
return config