Python socket.bind() Examples
The following are 30
code examples of socket.bind().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
socket
, or try the search function
.
Example #1
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 7 votes |
def _testTCPServerOption(self, level, option, values): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._testSetAndGetOption(sock, level, option, values) # now bind and listen on the socket i.e. cause the implementation socket to be created sock.bind( (HOST, PORT) ) sock.listen(50) retrieved_option_value = sock.getsockopt(level, option) msg = "TCP server option value '(%s,%s)'='%s' did not propagate to implementation socket. Got %s" % (level, option, values[-1], retrieved_option_value) if option == socket.SO_RCVBUF: # NOTE: see similar bsd/solaris workaround above self.assert_(retrieved_option_value >= values[-1], msg) else: self.failUnlessEqual(retrieved_option_value, values[-1], msg) self._testSetAndGetOption(sock, level, option, values) finally: sock.close()
Example #2
Source File: serving.py From recruit with Apache License 2.0 | 6 votes |
def get_sockaddr(host, port, family): """Return a fully qualified socket address that can be passed to :func:`socket.bind`.""" if family == af_unix: return host.split("://", 1)[1] try: res = socket.getaddrinfo( host, port, family, socket.SOCK_STREAM, socket.IPPROTO_TCP ) except socket.gaierror: return host, port return res[0][4]
Example #3
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 6 votes |
def testSO_ERROR(self): good = bad = None try: good = socket.socket(socket.AF_INET, socket.SOCK_STREAM) good.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) good.bind((HOST, PORT)) good.listen(1) bad = socket.socket(socket.AF_INET, socket.SOCK_STREAM) bad.bind((HOST, PORT)) bad.listen(1) self.fail("Listen operation against same port did not generate an expected error") except socket.error, se: self.failUnlessEqual(bad.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR), se[0]) # try again, should now be reset self.failUnlessEqual(bad.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR), 0)
Example #4
Source File: tcp.py From learn_python3_spider with MIT License | 6 votes |
def _resolveIPv6(ip, port): """ Resolve an IPv6 literal into an IPv6 address. This is necessary to resolve any embedded scope identifiers to the relevant C{sin6_scope_id} for use with C{socket.connect()}, C{socket.listen()}, or C{socket.bind()}; see U{RFC 3493 <https://tools.ietf.org/html/rfc3493>} for more information. @param ip: An IPv6 address literal. @type ip: C{str} @param port: A port number. @type port: C{int} @return: a 4-tuple of C{(host, port, flow, scope)}, suitable for use as an IPv6 address. @raise socket.gaierror: if either the IP or port is not numeric as it should be. """ return socket.getaddrinfo(ip, port, 0, 0, 0, _NUMERIC_ONLY)[0][4]
Example #5
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
def testSO_ERROR(self): good = bad = None try: good = socket.socket(socket.AF_INET, socket.SOCK_STREAM) good.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) good.bind((HOST, PORT)) good.listen(1) bad = socket.socket(socket.AF_INET, socket.SOCK_STREAM) bad.bind((HOST, PORT)) bad.listen(1) self.fail("Listen operation against same port did not generate an expected error") except socket.error, se: self.failUnlessEqual(bad.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR), se[0]) # try again, should now be reset self.failUnlessEqual(bad.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR), 0)
Example #6
Source File: tcp.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _resolveIPv6(ip, port): """ Resolve an IPv6 literal into an IPv6 address. This is necessary to resolve any embedded scope identifiers to the relevant C{sin6_scope_id} for use with C{socket.connect()}, C{socket.listen()}, or C{socket.bind()}; see U{RFC 3493 <https://tools.ietf.org/html/rfc3493>} for more information. @param ip: An IPv6 address literal. @type ip: C{str} @param port: A port number. @type port: C{int} @return: a 4-tuple of C{(host, port, flow, scope)}, suitable for use as an IPv6 address. @raise socket.gaierror: if either the IP or port is not numeric as it should be. """ return socket.getaddrinfo(ip, port, 0, 0, 0, _NUMERIC_ONLY)[0][4]
Example #7
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
def _testTCPServerOption(self, level, option, values): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._testSetAndGetOption(sock, level, option, values) # now bind and listen on the socket i.e. cause the implementation socket to be created sock.bind( (HOST, PORT) ) sock.listen(50) retrieved_option_value = sock.getsockopt(level, option) msg = "TCP server option value '(%s,%s)'='%s' did not propagate to implementation socket. Got %s" % (level, option, values[-1], retrieved_option_value) if option == socket.SO_RCVBUF: # NOTE: see similar bsd/solaris workaround above self.assert_(retrieved_option_value >= values[-1], msg) else: self.failUnlessEqual(retrieved_option_value, values[-1], msg) self._testSetAndGetOption(sock, level, option, values) finally: sock.close()
Example #8
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # This server is not needed for all tests, but create it anyway # It uses an ephemeral port, so there should be no port clashes or # problems with reuse. self.server_peer = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server_peer.bind( ("localhost", 0) ) self.server_peer.listen(5)
Example #9
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # This server is not needed for all tests, but create it anyway # It uses an ephemeral port, so there should be no port clashes or # problems with reuse. self.server_peer = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server_peer.bind( ("localhost", 0) ) self.server_peer.listen(5)
Example #10
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def _testConnectWithLocalBind(self): # Testing blocking connect with local bind cli_port = self.PORT - 1 start = time.time() while True: # Keep trying until a local port is available self.cli.settimeout(1) self.cli.bind( (self.HOST, cli_port) ) try: self.cli.connect((self.HOST, self.PORT)) break except socket.error, se: # cli_port is in use (maybe in TIME_WAIT state from a # previous test run). reset the client socket and try # again self.failUnlessEqual(se[0], errno.EADDRINUSE) print "Got an error in connect, will retry", se try: self.cli.close() except socket.error: pass self.clientSetUp() cli_port -= 1 # Make sure we have no tests currently holding open this socket test_support.gc_collect() if time.time() - start > 5: self.fail("Timed out after 5 seconds")
Example #11
Source File: socketservervideogenerator.py From Automatic-Youtube-Reddit-Text-To-Speech-Video-Generator-and-Uploader with MIT License | 5 votes |
def startVideoGeneratorServer(): server_address = (settings.server_location, int(settings.server_port_vid_gen)) print('Starting video generator server on %s port %s' % server_address) socket.bind(server_address) socket.listen(5) thread = Thread(target=waitConnect) thread.start() servertick = Thread(target=serverTick) servertick.start()
Example #12
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def _create_impl_socket(self): self.s.bind(("localhost", 0)) self.s.listen(5)
Example #13
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def testAddrTupleIDNAHostname(self): idna_domain = u"al\u00e1n.com" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if socket.supports('idna'): try: s.bind( (idna_domain, 80) ) except socket.error: # We're not worried about socket errors, i.e. bind problems, etc. pass except Exception, x: self.fail("Unexpected exception raised for socket.bind(%s)" % repr(idna_domain))
Example #14
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def testBindException(self): # First bind to the target port self.s.bind( (HOST, PORT) ) try: # And then try to bind again t = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) t.bind( (HOST, PORT) ) except socket.error, se: self.failUnlessEqual(se[0], errno.EADDRINUSE)
Example #15
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def testBindException(self): # First bind to the target port self.s.bind( (HOST, PORT) ) self.s.listen(50) try: # And then try to bind again t = socket.socket(socket.AF_INET, socket.SOCK_STREAM) t.bind( (HOST, PORT) ) t.listen(50) except socket.error, se: self.failUnlessEqual(se[0], errno.EADDRINUSE)
Example #16
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def testGetsocknameBoundNoImpl(self): self.s.bind( ("localhost", 0) ) try: self.s.getsockname() except socket.error, se: self.fail("getsockname() on bound socket should have not raised socket.error")
Example #17
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.serv.bind((self.HOST, self.PORT))
Example #18
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.serv.bind((self.HOST, self.PORT)) self.serv.listen(1)
Example #19
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def _create_impl_socket(self): # Binding is enough to cause socket impl creation self.s.bind(("localhost", 0))
Example #20
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def _create_impl_socket(self): self.s.bind(("localhost", 0)) self.s.listen(5)
Example #21
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def _testTCPClientInheritedOption(self, level, option, values): cli_sock = accepted_sock = None try: server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._testSetAndGetOption(server_sock, level, option, values) # now bind and listen on the socket i.e. cause the implementation socket to be created server_sock.bind( (HOST, PORT) ) server_sock.listen(50) # Now create client socket to connect to server cli_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) cli_sock.connect( (HOST, PORT) ) accepted_sock = server_sock.accept()[0] retrieved_option_value = accepted_sock.getsockopt(level, option) msg = "TCP client inherited option value '(%s,%s)'='%s' did not propagate to accepted socket: got %s" % (level, option, values[-1], retrieved_option_value) if option == socket.SO_RCVBUF: # NOTE: see similar bsd/solaris workaround above self.assert_(retrieved_option_value >= values[-1], msg) else: self.failUnlessEqual(retrieved_option_value, values[-1], msg) self._testSetAndGetOption(accepted_sock, level, option, values) finally: server_sock.close() time.sleep(1) if cli_sock: cli_sock.close() if accepted_sock: accepted_sock.close()
Example #22
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def _create_impl_socket(self): # Binding is enough to cause socket impl creation self.s.bind(("localhost", 0))
Example #23
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def testGetsocknameBoundNoImpl(self): self.s.bind( ("localhost", 0) ) try: self.s.getsockname() except socket.error, se: self.fail("getsockname() on bound socket should have not raised socket.error")
Example #24
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def testAddrTupleIDNAHostname(self): idna_domain = u"al\u00e1n.com" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if socket.supports('idna'): try: s.bind( (idna_domain, 80) ) except socket.error: # We're not worried about socket errors, i.e. bind problems, etc. pass except Exception, x: self.fail("Unexpected exception raised for socket.bind(%s)" % repr(idna_domain))
Example #25
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def testBindException(self): # First bind to the target port self.s.bind( (HOST, PORT) ) try: # And then try to bind again t = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) t.bind( (HOST, PORT) ) except socket.error, se: self.failUnlessEqual(se[0], errno.EADDRINUSE)
Example #26
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def testBindException(self): # First bind to the target port self.s.bind( (HOST, PORT) ) self.s.listen(50) try: # And then try to bind again t = socket.socket(socket.AF_INET, socket.SOCK_STREAM) t.bind( (HOST, PORT) ) t.listen(50) except socket.error, se: self.failUnlessEqual(se[0], errno.EADDRINUSE)
Example #27
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def _testConnectWithLocalBind(self): # Testing blocking connect with local bind cli_port = self.PORT - 1 start = time.time() while True: # Keep trying until a local port is available self.cli.settimeout(1) self.cli.bind( (self.HOST, cli_port) ) try: self.cli.connect((self.HOST, self.PORT)) break except socket.error, se: # cli_port is in use (maybe in TIME_WAIT state from a # previous test run). reset the client socket and try # again self.failUnlessEqual(se[0], errno.EADDRINUSE) print "Got an error in connect, will retry", se try: self.cli.close() except socket.error: pass self.clientSetUp() cli_port -= 1 # Make sure we have no tests currently holding open this socket test_support.gc_collect() if time.time() - start > 5: self.fail("Timed out after 5 seconds")
Example #28
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def testBroadcast(self): self.serv.bind( ("", self.PORT) ) msg = self.serv.recv(len(EIGHT_BIT_MSG)) self.assertEqual(msg, EIGHT_BIT_MSG)
Example #29
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def testShutdown(self): self.sock.bind( (self.HOST, self.PORT) ) self.sock.shutdown(socket.SHUT_RDWR)
Example #30
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def testBindSpecific(self): self.sock.bind( (self.HOST, self.PORT) ) # Use a specific port actual_port = self.sock.getsockname()[1] self.failUnless(actual_port == self.PORT, "Binding to specific port number should have returned same number: %d != %d" % (actual_port, self.PORT))