从 Python TCP Web 服务器打印出地址
Printing out addr from Python TCP WebServer
我能够打印出本地主机 127.0.0.1,很好,但是我无法打印出端口 61627,如下所示。正如这个 中提到的,我设置了 outputdata = serverSocket.getsockname()
和 serverSocket.getpeername()
但都未能 return 端口。
#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('127.0.0.1', 80))
serverSocket.listen(1)
while True:
print ('Ready to serve...')
connectionSocket, addr = serverSocket.accept()
print ('Required connection', addr)
try:
message = connectionSocket.recv(1024)
outputdata = addr
connectionSocket.send(('HTTP/1.0 200 OK\r\n\r\n').encode('utf-8'))
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i].encode('utf-8'))
connectionSocket.close()
except IOError:
connectionSocket.send(('404 Not Found').encode('utf-8'))
connectionSocket.close()
serverSocket.close()
考虑到上述脚本属于服务器,您可以尝试添加
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
绑定前
我删除了 for 循环并将 connectionSocket.send(outputdata[i].encode('utf-8'))
替换为 connectionSocket.sendall(str(outputdata).encode('utf-8'))
。我已将输出数据转换为字符串,sendall
return 是整个元组。我即兴 Python TCP WebServer 的最终代码是:
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serverSocket.bind(('127.0.0.1', 80))
serverSocket.listen(1)
while True:
print ('Ready to serve...')
connectionSocket, addr = serverSocket.accept()
print ('Required connection', addr)
try:
message = connectionSocket.recv(1024)
outputdata = addr
connectionSocket.send(('HTTP/1.0 200 OK\r\n\r\n').encode('utf-8'))
connectionSocket.sendall(str(outputdata).encode('utf-8'))
print(outputdata)
connectionSocket.close()
except IOError:
connectionSocket.send(('404 Not Found').encode('utf-8'))
connectionSocket.close()
serverSocket.close()
请问return这个到localhost
我能够打印出本地主机 127.0.0.1,很好,但是我无法打印出端口 61627,如下所示。正如这个 outputdata = serverSocket.getsockname()
和 serverSocket.getpeername()
但都未能 return 端口。
#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('127.0.0.1', 80))
serverSocket.listen(1)
while True:
print ('Ready to serve...')
connectionSocket, addr = serverSocket.accept()
print ('Required connection', addr)
try:
message = connectionSocket.recv(1024)
outputdata = addr
connectionSocket.send(('HTTP/1.0 200 OK\r\n\r\n').encode('utf-8'))
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i].encode('utf-8'))
connectionSocket.close()
except IOError:
connectionSocket.send(('404 Not Found').encode('utf-8'))
connectionSocket.close()
serverSocket.close()
考虑到上述脚本属于服务器,您可以尝试添加
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
绑定前
我删除了 for 循环并将 connectionSocket.send(outputdata[i].encode('utf-8'))
替换为 connectionSocket.sendall(str(outputdata).encode('utf-8'))
。我已将输出数据转换为字符串,sendall
return 是整个元组。我即兴 Python TCP WebServer 的最终代码是:
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serverSocket.bind(('127.0.0.1', 80))
serverSocket.listen(1)
while True:
print ('Ready to serve...')
connectionSocket, addr = serverSocket.accept()
print ('Required connection', addr)
try:
message = connectionSocket.recv(1024)
outputdata = addr
connectionSocket.send(('HTTP/1.0 200 OK\r\n\r\n').encode('utf-8'))
connectionSocket.sendall(str(outputdata).encode('utf-8'))
print(outputdata)
connectionSocket.close()
except IOError:
connectionSocket.send(('404 Not Found').encode('utf-8'))
connectionSocket.close()
serverSocket.close()
请问return这个到localhost