Why binding 0 port produces "Address already in use"? - python

I have this code:
socks = []
for i in range(20):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setblocking(False)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', 0))
socks.append(s)
# ...... Some code triggering s.connect_ex to calculate connection time ......
for s in socks:
s.shutdown(socket.SHUT_RDWR)
s.close()
Sometimes this code produces the following exception:
2022-07-04 16:06:59,147 ERROR - Thread 'Thread-239' failed
Traceback (most recent call last):
File "/root/connection_time/lib/time_socks.py", line 279, in time_socks
s.bind(('0.0.0.0', 0))
OSError: [Errno 98] Address already in use
Ps. The code in running as a root user.

Related

Hasseb USB DALI Master & python library python-dali

We have Hasseb USB Dali Master and try to used python library (github.com/sde1000/python-dali). But codes from section “Examples” don’t work.
We used Armbian Linux (Ubuntu based operating system)
The library https://github.com/onitake/daliserver was installed.
We try to start set_single.py separately.
$sudo python set_single.py
Traceback (most recent call last):
File "set_single.py", line 25, in <module> d.send(cmd)
File "build/bdist.linux-armv7l/egg/dali/driver/daliserver.py", line 43, in send
File "/usr/lib/python2.7/socket.py", line 575, in create_connection raise err
socket.error: [Errno 111] Connection refused
And try to start dalliserver in another terminal.
$ sudo python server.py
('Connection address_:', ('127.0.0.1', 43653))
Traceback (most recent call last):
File "server.py", line 14, in <module>
s.bind((TCP_IP, TCP_PORT))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use
(This error occurred when set_single.py started)
$ sudo python set_single.py
Traceback (most recent call last):
File "set_single.py", line 25, in <module>
d.send(cmd)
File "build/bdist.linux-armv7l/egg/dali/driver/daliserver.py", line 66, in send
File "build/bdist.linux-armv7l/egg/dali/driver/daliserver.py", line 84, in unpack_response
struct.error: unpack requires a string argument of length 4
file set_single.py
from dali.address import Broadcast
from dali.address import Short
from dali.gear.general import DAPC
from dali.driver.daliserver import DaliServer
import sys
if __name__ == "__main__":
addr = Broadcast()
level = int(150)
d = DaliServer("localhost", 55825 )
cmd = DAPC(addr, level)
d.send(cmd)
file server.py
import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 55825
BUFFER_SIZE = 20
while True:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
try:
print("Connection address_:", addr)
while 1:
conn.setblocking(0)
conn.settimeout(20.0)
data = conn.recv(BUFFER_SIZE)
if not data:
break
stream = ":".join("{:02x}".format(ord(chr(c))) for c in data)
print("received data: [{1}] {0}".format(stream, len(data)))
conn.send(b"\x02\xff\x00\x00")
except:
pass
conn.close()

server.bind(), only local ip address

I can't define a server with the real IP address of my computer, it gives me an error.When I do this with my local IP address it works but I want to run the client in other computer
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('my_ip',9876))
server_socket.listen(1)
(client_socket,client_address)=server_socket.accept()
recived_data=client_socket.recv(999999999)
password=open(r'D:\passwords','wb')
password.write(recived_data)
client_socket.close()
server_socket.close()
the client:
import socket
my_socket = socket.socket()
my_socket.connect(('the_real_ip', 9876))
the error I get is:
Traceback (most recent call last):
File "D:/Heights/Documents/Projects/Cyber/Password_Server.py", line 4, in <module>
server_socket.bind(('my_ip',9876))
File "D:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10049] The requested address is not valid in its context
You can use server_socket.bind(('0.0.0.0',9876)) and server_socket.bind(('',9876))
Check this link: https://serverfault.com/questions/78048/whats-the-difference-between-ip-address-0-0-0-0-and-127-0-0-1
To connect from outside your LAN, you have to connect to the router of that LAN using the 'outside IP' and make let the router formward the connection to the 'local IP'.

AttributeError from client sockets in python

I'm getting an attribute error from running the client side of the program, I'm pretty sure I did it everything correctly but apparently not.
Here's the code:
from socket import *
serverName = 'hostname'
serverPort = 12000
clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM)
message = raw_input('Input lowercase sentence:')
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()
This is the error I get:
Traceback (most recent call last):
File "UDPClient.py", line 4, in <module>
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
AttributeError: type object '_socketobject' has no attribute 'socket'
EDIT:
Traceback (most recent call last):
File "UDPClient.py", line 6, in <module>
clientSocket.sendto(message,(serverName,serverPort))
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
You're getting this wrong. Since you import *, just use AF_INET and SOCK_DGRAM
>>> from socket import *
>>> clientSocket = socket(AF_INET, SOCK_DGRAM)
Tested on my machine using Py3.4

Socket connection won't close?

I'm trying to write a simple server program that prints the data sent to it, or quits if the data is "quit", "exit", or "stop":
HOST = ""
PORT = 37720
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen(1)
while(1):
conn, addr = server.accept()
data = conn.recv(1024)
if not data: break
inData, inUrl = json.loads(data)
if inData == "quit" or inData == "exit" or inData == "stop":
print("Quitting")
conn.close()
break
else:
conn.send("Received")
print(inData)
It does what I expect, except that when I try to run the server again once it's quit, I get:
Traceback (most recent call last):
File "./s", line 14, in <module>
server.bind((HOST, PORT))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use
I assume this means the connection wasn't closed. How can I close it to prevent this? I tried the conn.close(), but it didn't fix anything.
You need to set SO_REUSEADDR flag.
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

Python socket error - how to repair

I have this code:
>>> import socket
>>> sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> sck.connect(('loaclhost', 2525))
And I have this error:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
sck.connect(('localhost', 2526))
socket.error: [Errno 10061] No connection could be made because
the target machine actively refused it
Please helpe me. Where can be error? How can I repair it?
try changing the connection to "localhost" instead of "loaclhost"
Your code and error don't match (spelling of localhost and port number), but the error message is due to no server listening on that port. Cut and paste the exact code and error message next time.

Categories