在学习网络编程的过程中遇到了这个问题。我去stackoverflow去搜寻是否有类似的问题。但是没有找到遇到这样的问题的。好多都是遇到Socket has no attribute AF_INET的问题。机器环境是:win7 32bit python版本是2.7.8
here is the code:
# -*- coding: utf-8 -*-
import socket
from time import ctime
host = "127.0.0.1"
port = 8888
buf = 1024
ADDR = (host, port)
tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpSocket.bind(ADDR)
tcpSocket.listen(5)
try:
while True:
#进入服务器的无限循环中,等待连接到来
#有连接时,进入对话循环,等待客户发送数据。
#如果数据为空,表示客户端已经退出。等待下一个客户端连接。
#得到客户端消息后在消息前加一个时间戳后返回
print "waiting for connecting....."
conn, addr = tcpSocket.accept()
print "the data from ", addr
#进入会话循环
while True:
#接收客户端数据
data = tcpSocket.recv(buf)
if not data:
break
tcpSocket.send("[%s] %s" % (ctime, data))
except Exception, e:
tcpSocket.close()
报错的具体信息如下:
Any help please.....
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你的文件名字是不是叫socket.py
python自动把当前目录的py文件当作模块来import了!
2.7.8 版本有问题 换成2.7.6 吧 或者是你装了多个版本的py 环境变量被覆盖了 指定py 版本运行