Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import module from socket error python

Tags:

python

sockets

when i try to create af socket with the import socket module like:

from socket import *
from thread import *
responseok = bytes('ok')
HOST = ''
PORT = 4445
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.bind((HOST, PORT))
c.listen(10)

def clientthread(conn):

   dat = conn.recv(1024)
   data = str(dat)
   print data
   conn.close()


while 1:
conn, addr = c.accept()
start_new_thread(clientthread ,(conn,))

c.close()

i get the following error:

Traceback (most recent call last):
  File "C:\Users\MikeClaudi\My Documents\LiClipse Workspace\server2\src\socket.py", line 6, in    <module>
from socket import *
  File "C:\Users\MikeClaudi\My Documents\LiClipse Workspace\server2\src\socket.py", line 11, in <module>
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

NameError: name 'socket' is not defined

like image 911
Claudi Avatar asked May 07 '26 21:05

Claudi


1 Answers

If you use

from socket import *

then you have to do

c = socket(socket.AF_INET, socket.SOCK_STREAM)

But in order to not confuse the two socket (module and class), just

import socket

and then

c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
like image 119
fredtantini Avatar answered May 10 '26 09:05

fredtantini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!