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

时间:2017-01-08 11:14:41

标签: python python-2.7 sockets network-programming

我是套接字或网络编程的新手。我只是想创建一个套接字,但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'

1 个答案:

答案 0 :(得分:6)

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