报错信息:
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>
我只能怀疑你自己写了个文件叫socket.py了<divclass='ref'>
有一个中招的。。。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。