python 遇到这个问题:
源码如下
# 导入 socket、sys 模块
import socket
import sys
# 创建 socket 对象
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
port = 80
serversocket.bind(('127.0.0.1', port))
# 设置最大连接数,超过后排队
serversocket.listen(5)
while True:
# 建立客户端连接
clientsocket,addr = serversocket.accept()
print("连接地址: %s" %str(addr))
str = clientsocket.recv(1024)
print(str1)
#msg='欢迎访问菜鸟教程!'+ "\r\n"
msg = 'HTTP/1.1 200 OK\r\nContent-Type: text/html;charset=ISO-8859-1\r\nContent-Length: 500\r\n\r\n<html>\r\n<head>\r\n<title> hello word it is my first try</title>\r\n</head>\r\n<body>\r\n<h2>\r\n hello word it is my first try</h2>\r\n</body>\r\n</html>\r\n'
clientsocket.send(msg.encode('utf-8'))
print("dfsdf")
clientsocket.close()
#while True:
# pass
报错如下:
raceback (most recent call last):
File "E:\Data\Coding\python\socke.py", line 17, in <module>
print("连接地址: %s" %str(addr))
TypeError: 'bytes' object is not callable
因为这个函数里调用了str(addr),而后面定义了字符串str.循环执行第二次的时候会报这个错,
这时候把str 当bytes,报错,该对象不可调用(不是函数)