0
Я хочу написать HTTP-клиент для тестирования веб-сервера. Клиент будет использовать TCP-соединение. входSocket.error: [Errno 61] Connection failed Python
#Import socket module
from socket import *
import sys
server_host = sys.argv[1]
server_port = int(sys.argv[2])
filename = sys.argv[3]
clientSocket = socket(AF_INET,SOCK_STREAM)
clientSocket.connect((server_host,server_port))
while True:
print 'Ready to server...'
connectionSocket, addr = clientSocket.accept()
print 'connected from', addr
try:
f = open(filename[1:])
outputdata = f.read()
#Send one HTTP header line into socket
connectionSocket.sendall('\nHTTP/1.1 200 OK\r\n\r\n')
#Send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.sendall(outputdata[i])
#Closes the socket for this client
connectionSocket.close()
print 'File Received'
except IOError:
print 'IOError'
#Send response message for file not found
connectionSocket.sendall('\n404 Not Found\n')
#Close client socket
connectionSocket.close()
clientSocket.close()
Команда Линд:
python ./client.py 127.0.0.1 8000 HelloWorld.html
Этот сценарий выводит:
Traceback (most recent call last):File "./client.py", line 9, in <module>
clientSocket.connect((server_host,server_port))
File"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 61] Connection refused
Я бегу на Python 2.7.11 на Mac OS X.
я пытался поиск вокруг много не получил решения.
Есть ли веб-сервер, который прослушивает порт 8000? – Selcuk
@ Selcuk Это должно быть. – Echo0831
Вы не уверены? Можете ли вы подключиться к 'http: //127.0.0.1: 8000' с помощью веб-браузера? – Selcuk