Socket does not work in Python

I tried the socket module in Python, but whenever I try to run this code:

import socket import sys host = '192.168.1.1' port = 23 try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) except: print "socket() failed" sys.exit(1) 

Then he dies. Here is the error without exception and try:

 Traceback (most recent call last): File "C:\Documents and Settings\a\Desktop\socket.py", line 1, in <module> import socket File "C:\Documents and Settings\a\Desktop\socket.py", line 6, in <module> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) AttributeError: 'module' object has no attribute 'AF_INET' 

I just started Python (Today) and I need help.

I saved it as socket.py, but also sock.py ...

+4
source share
1 answer
 import socket 

scans the current directory in front of the standard Python library . And since your file is called socket.py , it is imported instead of the standard socket library module. Rename (do not copy, as this will leave the original file) to a file for something else, for example my_socket.py .

+13
source

Source: https://habr.com/ru/post/1446517/


All Articles