本文整理汇总了Python中socket.error方法的典型用法代码示例。如果您正苦于以下问题:Python socket.error方法的具体用法?Python socket.error怎么用?Python socket.error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket
的用法示例。
在下文中一共展示了socket.error方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def __init__(self, eventHandler):
threading.Thread.__init__(self)
self.name = 'Server'
self.daemon = True
self._eventHandler = eventHandler
self._client = None
self._server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
self._server.bind(('', PORT))
self._server.listen(5)
except socket.error:
event = Event(Events.SCAN_ERROR, msg='Could not start server')
post_event(eventHandler, event)
return
self._cancel = False
self.start()
示例2: run
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def run(self):
while not self._cancel:
if self._client is None:
endpoints = [self._server]
else:
endpoints = [self._server, self._client]
read, _write, error = select.select(endpoints, [], [], 0.5)
for sock in read:
if sock is self._server:
try:
client, _addr = self._server.accept()
if self._client is not None:
self._client.close()
self._client = client
except socket.error:
self._client = None
for sock in error:
if sock is self._client:
self._client = None
sock.close()
示例3: test_Content_Length_in
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def test_Content_Length_in(self):
# Try a non-chunked request where Content-Length exceeds
# server.max_request_body_size. Assert error before body send.
self.persistent = True
conn = self.HTTP_CONN
conn.putrequest('POST', '/upload', skip_host=True)
conn.putheader('Host', self.HOST)
conn.putheader('Content-Type', 'text/plain')
conn.putheader('Content-Length', '9999')
conn.endheaders()
response = conn.getresponse()
self.status, self.headers, self.body = webtest.shb(response)
self.assertStatus(413)
self.assertBody('The entity sent with the request exceeds '
'the maximum allowed bytes.')
conn.close()
示例4: test_http_over_https
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def test_http_over_https(self):
if self.scheme != 'https':
return self.skip('skipped (not running HTTPS)... ')
# Try connecting without SSL.
conn = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
conn.putrequest('GET', '/', skip_host=True)
conn.putheader('Host', self.HOST)
conn.endheaders()
response = conn.response_class(conn.sock, method='GET')
try:
response.begin()
self.assertEqual(response.status, 400)
self.body = response.read()
self.assertBody('The client sent a plain HTTP request, but this '
'server only speaks HTTPS on this port.')
except socket.error:
e = sys.exc_info()[1]
# "Connection reset by peer" is also acceptable.
if e.errno != errno.ECONNRESET:
raise
示例5: test_garbage_in
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def test_garbage_in(self):
# Connect without SSL regardless of server.scheme
c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
c._output(b'gjkgjklsgjklsgjkljklsg')
c._send_output()
response = c.response_class(c.sock, method='GET')
try:
response.begin()
self.assertEqual(response.status, 400)
self.assertEqual(response.fp.read(22),
b'Malformed Request-Line')
c.close()
except socket.error:
e = sys.exc_info()[1]
# "Connection reset by peer" is also acceptable.
if e.errno != errno.ECONNRESET:
raise
示例6: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def __init__(self, address, port, key, logger, sname):
self.address = address
self.port = int(port)
self.key = key
self.logger = logger
self.serverName = sname
while True: # keep going until we break out inside the loop
try:
self.logger.debug('Attempting to connect to '+self.serverName+' server at '+str(self.address)+' port '+str(self.port))
self.conn = Client((self.address, self.port))
self.logger.debug('Connect to '+self.serverName+' successful.')
break
except SocketError as serr:
if serr.errno == errno.ECONNREFUSED:
self.logger.debug('Connect to '+self.serverName+' failed because connection was refused (the server is down). Trying again.')
else:
# Not a recognized error. Treat as fatal.
self.logger.debug('Connect to '+self.serverName+' gave socket error '+str(serr.errno))
raise serr
except:
self.logger.exception('Connect to '+self.serverName+' threw unknown exception')
raise
示例7: send
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def send(self, msg):
# TODO: Busy wait will do for initial startup but for dealing with server down in the middle of things
# TODO: then busy wait is probably inappropriate.
while True: # keep going until we break out inside the loop
try:
self.logger.debug('Attempting to connect to '+self.serverName+' server at '+str(self.address)+' port '+str(self.port))
conn = Client((self.address, self.port))
self.logger.debug('Connect to '+self.serverName+' successful.')
break
except SocketError as serr:
if serr.errno == errno.ECONNREFUSED:
self.logger.debug('Connect to '+self.serverName+' failed because connection was refused (the server is down). Trying again.')
else:
# Not a recognized error. Treat as fatal.
self.logger.debug('Connect to '+self.serverName+' gave socket error '+str(serr.errno))
raise serr
except:
self.logger.exception('Connect to '+self.serverName+' threw unknown exception')
raise
conn.send(msg)
conn.close()
示例8: create_command_listener
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def create_command_listener (baddr, port):
try:
if port is None:
try:
if os.path.exists(baddr):
os.unlink(baddr)
except OSError:
print 'could not remove old unix socket ' + baddr
return
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) # @UndefinedVariable
s.bind(baddr)
else:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((baddr, int(port)))
except socket.error , msg:
print 'Bind failed on command interface ' + baddr + ' port ' + str(port) + ' Error Code : ' + str(msg[0]) + ' Message ' + msg[1] + '\n'
return
示例9: validate_optional_args
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def validate_optional_args(args):
"""Check if an argument was provided that depends on a module that may
not be part of the Python standard library.
If such an argument is supplied, and the module does not exist, exit
with an error stating which module is missing.
"""
optional_args = {
'json': ('json/simplejson python module', json),
'secure': ('SSL support', HTTPSConnection),
}
for arg, info in optional_args.items():
if getattr(args, arg, False) and info[1] is None:
raise SystemExit('%s is not installed. --%s is '
'unavailable' % (info[0], arg))
示例10: send
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def send(self, channel_type, message, additional_data):
if channel_type == common.CONTROL_CHANNEL_BYTE:
transformed_message = self.transform(self.encryption, common.CONTROL_CHANNEL_BYTE+message, 1)
else:
transformed_message = self.transform(self.encryption, common.DATA_CHANNEL_BYTE+message, 1)
common.internal_print("{0} sent: {1}".format(self.module_short, len(transformed_message)), 0, self.verbosity, common.DEBUG)
# WORKAROUND?!
# Windows: It looks like when the buffer fills up the OS does not do
# congestion control, instead throws and exception/returns with
# WSAEWOULDBLOCK which means that we need to try it again later.
# So we sleep 100ms and hope that the buffer has more space for us.
# If it does then it sends the data, otherwise tries it in an infinite
# loop...
while True:
try:
return self.comms_socket.send(struct.pack(">H", len(transformed_message))+transformed_message)
except socket.error as se:
if se.args[0] == 10035: # WSAEWOULDBLOCK
time.sleep(0.1)
pass
else:
raise
示例11: check
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def check(self):
try:
common.internal_print("Checking module on server: {0}".format(self.get_module_name()))
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.settimeout(3)
server_socket.connect((self.config.get("Global", "remoteserverip"), int(self.config.get(self.get_module_configname(), "serverport"))))
client_fake_thread = TCP_generic_thread(0, 0, None, None, server_socket, None, self.authentication, self.encryption_module, self.verbosity, self.config, self.get_module_name())
client_fake_thread.do_check()
client_fake_thread.communication(True)
self.cleanup(server_socket)
except socket.timeout:
common.internal_print("Checking failed: {0}".format(self.get_module_name()), -1)
self.cleanup(server_socket)
except socket.error as exception:
if exception.args[0] == 111:
common.internal_print("Checking failed: {0}".format(self.get_module_name()), -1)
else:
common.internal_print("Connection error: {0}".format(self.get_module_name()), -1)
self.cleanup(server_socket)
return
示例12: check
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def check(self):
try:
common.internal_print("Checking module on server: {0}".format(self.get_module_name()))
server_socket = self.sctp.sctpsocket_tcp(socket.AF_INET)
server_socket.settimeout(3)
server_socket.connect((self.config.get("Global", "remoteserverip"), int(self.config.get(self.get_module_configname(), "serverport"))))
client_fake_thread = SCTP_generic_thread(0, 0, None, None, server_socket, None, self.authentication, self.encryption_module, self.verbosity, self.config, self.get_module_name())
client_fake_thread.do_check()
client_fake_thread.communication(True)
self.cleanup(server_socket)
except socket.timeout:
common.internal_print("Checking failed: {0}".format(self.get_module_name()), -1)
self.cleanup(server_socket)
except socket.error as exception:
if exception.args[0] == 111:
common.internal_print("Checking failed: {0}".format(self.get_module_name()), -1)
else:
common.internal_print("Connection error: {0}".format(self.get_module_name()), -1)
self.cleanup(server_socket)
return
示例13: communication_initialization
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def communication_initialization(self):
try:
common.internal_print("Waiting for upgrade request", 0, self.verbosity, common.DEBUG)
response = self.comms_socket.recv(4096)
if len(response) == 0:
common.internal_print("Connection was dropped", 0, self.verbosity, common.DEBUG)
self.cleanup()
sys.exit(-1)
handshake_key = self.WebSocket_proto.get_handshake_init(response)
if handshake_key == None:
common.internal_print("No WebSocket-Key in request", -1, self.verbosity, common.DEBUG)
self.cleanup()
sys.exit(-1)
handshake = self.WebSocket_proto.calculate_handshake(handshake_key)
response = self.WebSocket_proto.switching_protocol(handshake)
self.comms_socket.send(response)
except:
common.internal_print("Socket error", -1, self.verbosity, common.DEBUG)
self.cleanup()
sys.exit(-1)
return
示例14: connect
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def connect(self):
try:
common.internal_print("Starting client: {0}".format(self.get_module_name()))
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.server_tuple = (self.config.get("Global", "remoteserverip"), int(self.config.get(self.get_module_configname(), "serverport")))
self.comms_socket = server_socket
self.serverorclient = 0
self.authenticated = False
self.do_hello()
self.communication(False)
except KeyboardInterrupt:
self.do_logoff()
self.cleanup()
raise
except socket.error:
self.cleanup()
raise
self.cleanup()
return
示例15: check
# 需要导入模块: import socket [as 别名]
# 或者: from socket import error [as 别名]
def check(self):
try:
common.internal_print("Checking module on server: {0}".format(self.get_module_name()))
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.server_tuple = (self.config.get("Global", "remoteserverip"), int(self.config.get(self.get_module_configname(), "serverport")))
self.comms_socket = server_socket
self.serverorclient = 0
self.authenticated = False
self.do_check()
self.communication(True)
except KeyboardInterrupt:
self.cleanup()
raise
except socket.timeout:
common.internal_print("Checking failed: {0}".format(self.get_module_name()), -1)
except socket.error:
self.cleanup()
raise
self.cleanup()
return