开发者社区> 问答> 正文

mac 下python import总是报错?报错

报错信息:

wangyujiadeMacBook-Pro:documents wangyujia$ python server.py

<p style="font-size:11px;font-family:Menlo;">
	<span style="line-height:1.5;">Traceback (most recent call last):</span>
</p>
<p style="font-size:11px;font-family:Menlo;">
	  File "server.py", line 23, in <module>
</p>
<p style="font-size:11px;font-family:Menlo;">
	    getInfo()
</p>
<p style="font-size:11px;font-family:Menlo;">
	  File "server.py", line 7, in getInfo
</p>
<p style="font-size:11px;font-family:Menlo;">
	    sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
</p>
<p style="font-size:11px;font-family:Menlo;">
	AttributeError: 'module' object has no attribute 'AF_INET'
</p>
<p style="font-size:11px;font-family:Menlo;">
	wangyujiadeMacBook-Pro:documents wangyujia$ python server.py
</p>
<p style="font-size:11px;font-family:Menlo;">
	Traceback (most recent call last):
</p>
<p style="font-size:11px;font-family:Menlo;">
	  File "server.py", line 23, in <module>
</p>
<p style="font-size:11px;font-family:Menlo;">
	    getInfo()
</p>
<p style="font-size:11px;font-family:Menlo;">
	  File "server.py", line 7, in getInfo
</p>
<p style="font-size:11px;font-family:Menlo;">
	    sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
</p>
<p style="font-size:11px;font-family:Menlo;">
	AttributeError: 'module' object has no attribute 'AF_INET'
</p>

server.py 源码:

#!/usr/bin/env python

<p style="font-size:11px;font-family:Menlo;color:#008400;">
	#-*-coding:utf-8-*-
</p>
<p style="font-size:11px;font-family:Menlo;">
	<br>
</p>
<p style="font-size:11px;font-family:Menlo;">
	import socket
</p>
<p style="font-size:11px;font-family:Menlo;">
	def getInfo():
</p>
<p style="font-size:11px;font-family:Menlo;color:#272AD8;">
	<span> </span>address=('127.0.0.1',8888)
</p>
<p style="font-size:11px;font-family:Menlo;">
	<span> </span>sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
</p>
<p style="font-size:11px;font-family:Menlo;">
	<span> </span>sock.bind(address)
</p>
<p style="font-size:11px;font-family:Menlo;">
	<span> </span>sock.listen(5)
</p>
<p style="font-size:11px;font-family:Menlo;">
	<span> </span>
</p>
<p style="font-size:11px;font-family:Menlo;">
	<span> </span>client,addr=sock.accept()
</p>
<p style="font-size:11px;font-family:Menlo;color:#272AD8;">
	<span> </span>print 'connected with',addr
</p>
<p style="font-size:11px;font-family:Menlo;">
	<span> </span>
</p>
<p style="font-size:11px;font-family:Menlo;color:#272AD8;">
	<span> </span>client.send(b'Hi I am server!')
</p>
<p style="font-size:11px;font-family:Menlo;">
	<span> </span>ra=sock.recv(1024)
</p>
<p style="font-size:11px;font-family:Menlo;color:#BB2CA2;">
	<span> </span>print ra
</p>
<p style="font-size:11px;font-family:Menlo;">
	<span> </span>
</p>
<p style="font-size:11px;font-family:Menlo;">
	<span> </span>client.close()
</p>
<p style="font-size:11px;font-family:Menlo;">
	<span> </span>sock.close()
</p>
<p style="font-size:11px;font-family:Menlo;color:#008400;">
	#end def
</p>
<p style="font-size:11px;font-family:Menlo;">
	<br>
</p>
<p style="font-size:11px;font-family:Menlo;">
	if __name__=='__main__':
</p>
<p style="font-size:11px;font-family:Menlo;">
	<span> </span>getInfo()
</p>

展开
收起
爱吃鱼的程序员 2020-06-12 14:13:12 634 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    我只能怀疑你自己写了个文件叫socket.py了<divclass='ref'>

    引用来自“Altman”的评论

    我只能怀疑你自己写了个文件叫socket.py了

    有一个中招的。。。

    2020-06-12 14:13:29
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载