Python不发送所有数据,仅4kb
我试图创建一个Python web服务器,但是它似乎无法发送任何大于4KB的文件。如果文件的大小超过4KB,它只会将文本/图像的末尾截断超过4KB。从其他来源(AmazonS3/Twitter)嵌入的任何东西都可以正常工作 这是服务器代码。现在有点乱,但我专注于让它发挥作用。之后,我将为代码添加更多安全性Python不发送所有数据,仅4kb,python,sockets,web,server,Python,Sockets,Web,Server,我试图创建一个Python web服务器,但是它似乎无法发送任何大于4KB的文件。如果文件的大小超过4KB,它只会将文本/图像的末尾截断超过4KB。从其他来源(AmazonS3/Twitter)嵌入的任何东西都可以正常工作 这是服务器代码。现在有点乱,但我专注于让它发挥作用。之后,我将为代码添加更多安全性 ''' Simple socket server using threads ''' import socket import sys import time import os f
'''
Simple socket server using threads
'''
import socket
import sys
import time
import os
from thread import *
HOST = '' # Symbolic name meaning all available interfaces
PORT = 80 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
#Function for handling connections. This will be used to create threads
def clientthread(conn):
#Sending message to connected client
#infinite loop so that function do not terminate and thread do not end.
while True:
#Receiving from client
data = conn.recv(4096)
print data
dataSplit = data.split(' ')
print dataSplit
contentType = "text/html"
if(dataSplit[1].endswith(".html")):
print "HTML FILE DETECTED"
contentType = "text/html"
elif(dataSplit[1].endswith(".png")):
print "PNG FILE DETECTED"
contentType = "image/png"
elif(dataSplit[1].endswith(".css")):
print "CSS FILE DETECTED"
contentType = "text/css"
else:
print "NO MIMETYPE DEFINED"
conn.sendall('HTTP/1.1 200 OK\nServer: TestWebServ/0.0.1\nContent-Length: ' + str(os.path.getsize('index.html')) + '\nConnection: close\nContent-Type:' + contentType + '\n\n')
print '\n\n\n\n\n\n\n\n'
with open(dataSplit[1][1:]) as f:
fileText = f.read()
n = 1000
fileSplitToSend = [fileText[i:i+n] for i in range(0, len(fileText), n)]
for lineToSend in fileSplitToSend:
conn.sendall(lineToSend)
break
if not data:
break
#came out of loop
conn.close()
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(clientthread ,(conn,))
s.close
感谢您抽出时间。因此,感谢您用户“您”我们发现了问题。我有这个密码:
conn.sendall('HTTP/1.1 200 OK\nServer: TestWebServ/0.0.1\nContent-Length: ' + str(os.path.getsize('index.html')) + '\nConnection: close\nContent-Type:' + contentType + '\n\n')
而不是此代码:
conn.sendall('HTTP/1.1 200 OK\nServer: TestWebServ/0.0.1\nContent-Length: ' + str(os.path.getsize(dataSplit[1][1:])) + '\nConnection: close\nContent-Type:' + contentType + '\n\n')
问题是我发送每个文件的index.html文件大小,所以Chrome和其他浏览器只是删除了额外的数据。碰巧index.html是4KB,所以我认为这是一个数据包限制或该区域的某个地方。可能是传输编码=“chunked”会有帮助。我尝试更改传输编码,但现在Chrome甚至不会显示任何内容。它声称该网页在控制台中没有任何内容时不可用。作为参考,新字符串是
conn.sendall('HTTP/1.1 200 OK\n服务器:TestWebServ/0.0.1\n传输编码:chunked\n连接:close\n内容类型:'+contentType+'\n\n')
我选中了,它试图将内容发送到Chrome,但由于某种原因Chrome看不到内容。如果我继续使用内容长度系统,它将发送文件,但只发送前4KB。如果我尝试一次发送整个文件,它仍然只接收Chrome中的前4KB。我认为问题在于您硬编码了-os.path.getsize('index.html'),更改为dataSplit[1][1:]与您原来的代码一起工作。谢谢。这解决了我的问题。我不敢相信我犯了那个错误,却忽略了它。