Python3 Site 127.0.0.1 sent invalid response
When I first started learning python socket
. Then I immediately wrote my own tcp server similar to others. My old code, which worked all the time on python 3.4.4
, now I have python 3.6.1
installed .
Mistake:
Страница недоступна
Сайт 127.0.0.1 отправил недействительный ответ.
ERR_INVALID_HTTP_RESPONSE
Code from an old project https://github.com/Danchick1337/Punch-Network:
# -*- coding: utf-8 -*-
#* Import all the necessary modules and continuation of the code
from time import gmtime, strftime
import socket, requests
import sys, os, time
support_thread = False #*thread not support
main_file = 'punch-network.py' #main-file not other file 'main'
def tcp_server(): #server code socket
try:
global data, getHost, getPort, os, buffer, timed, full, clear
os = 'windows' ; os_linux_machine = 'unix'
true_status = 200 ; false_status = 404
buffer = 1024 ; timed = strftime("%Y:%H:%M:%S", gmtime()) ; clear = 'cls'
getHost = '127.0.0.1' ; getPort = int(sys.argv[1]) ; start = sys.argv[2]
full = (getHost, getPort)
if start == '-run_server':
print(' Punch-Network Started at '+'['+timed+']\r')
print(' -- Punch-Network Log --\r\n')
try:
punch = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
punch.bind(full) ; punch.listen(5)
while True:
conn, full = punch.accept()
data = conn.recv(buffer)
if not data:
only_data_error = True
if only_data_error:
print('error')
#handling module
else:
conn.sendall(b"""
<html>
<head>
<title>Punch-Network</title>
</head>
<body>
<h3>Punch-Network Version 0.BETA Worked!</h3>
</body>
</html>
""")
pass
punch.close()
except socket.herror as herr:
print('error')
except socket.timeout as terr:
print('error')
except socket.gaierror as gerr:
print('error')
else:
print('Error options, true-options: punch-network.py port -run_server')
tcp_server()
Removed all the extra modules. The most common tcp server with only a modified interface. Previously, everything worked, I went to 127.0.0.1:порт
and I already displayed that everything it works. Now, neither the server nor the client are working.
0
Author: you have no pass , 2017-05-08
1 answers
Before sending the page content, for the correct processing by the browser, you need to add a header that looks something like this:
conn.send(b"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
2
Author: Dmitry Erohin, 2017-05-12 21:48:51