AttributeError: type object 'socket' has no attribute 'socket'

im python to make a simple script to connect to another socket server but im having an issue in getting my servers ip using socket before i used socket.getbyhostname(socket.gethostname()) but im getting the error `AttributeError: ‘socket’ object has no attribute ‘gethostname’

ive tried importing everything from socket by using from socket import * but now im getting the error type object 'socket' has no attribute 'socket'

can someone explain whats going on and why this is a problem for me in the firstplace as ive used the same line before with no issues

Full code:

import socket
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOSTNAME = socket.getbyhostname(socket.gethostname())
#constantly wait for a connection
print("Waiting for a connection...")
while True:
      #listen for a connection
      socket.bind(("", 9998))
      conn, addr = socket.accept()
      print("Connected to: ", addr[0])

On this line you’ve imported the module and assigned it to the variable socket.

On this line you’ve reassigned a socket object to the variable socket. Now you have no more access to the module. Use a different name for these two things.

1 Like

Another problem is here:

HOSTNAME = socket.getbyhostname(socket.gethostname())

The method is gethostbyname, rather than getbyhostname.

See socket.gethostbyname(hostname).

Ohhhh okay ive got it now thanks for your help :slight_smile: