gpt4 book ai didi

python - 在 Python 2.7 中创建套接字时出错

转载 作者:太空宇宙 更新时间:2023-11-04 08:00:13 25 4
gpt4 key购买 nike

我对套接字或网络编程还很陌生。我只是想创建一个套接字,但 python 给了我回溯。我在 Ubuntu 12 上使用 python 2.7

我的代码:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'OK'

回溯是:

    Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in <module>
from apport.report import Report
File "/usr/lib/python2.7/dist-packages/apport/report.py", line 12, in <module>
import subprocess, tempfile, os.path, urllib, re, pwd, grp, os
File "/usr/lib/python2.7/urllib.py", line 26, in <module>
import socket
File "/home/ans/Desktop/python_p/socket.py", line 2, in <module>
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
AttributeError: 'module' object has no attribute 'AF_INET'

Original exception was:
Traceback (most recent call last):
File "socket.py", line 1, in <module>
import socket
File "/home/ans/Desktop/python_p/socket.py", line 2, in <module>
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
AttributeError: 'module' object has no attribute 'AF_INET'

最佳答案

您将您的程序文件命名为 socket.py,因此将导入此文件,而不是标准库模块。重命名您的文件,删除任何 socket.pyc 并重试。

关于python - 在 Python 2.7 中创建套接字时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41532092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com