Python Socket 问题:如何可靠获取来自任意浏览器的 POST 数据?
我写了一些小的Python+Ajax程序(在最后列出),使用了socket模块来研究COMET这个异步通信的概念。
这个想法是让浏览器能够通过我的Python程序实时互相发送消息。
关键在于保持“GET messages/...”这个连接打开,等待消息的回复。
我遇到的问题主要是关于通过socket.recv接收到的数据的可靠性...
当我从Firefox发送POST请求时,一切都运作良好。
但是当我从Chrome或IE发送POST请求时,我在Python中得到的“数据”是空的。
有没有人知道不同浏览器之间的这个问题?
是不是某些浏览器在发送数据时注入了一些EOF或其他字符,导致“recv”无法接收到数据?
有没有人知道解决这个问题的方法?
Python中的server.py:
import socket
connected={}
def inRequest(text):
content=''
if text[0:3]=='GET':
method='GET'
else:
method='POST'
k=len(text)-1
while k>0 and text[k]!='\n' and text[k]!='\r':
k=k-1
content=text[k+1:]
text=text[text.index(' ')+1:]
url=text[:text.index(' ')]
return {"method":method,"url":url,"content":content}
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
mySocket.bind ( ( '', 80 ) )
mySocket.listen ( 10 )
while True:
channel, details = mySocket.accept()
data=channel.recv(4096)
req=inRequest(data)
url=req["url"]
if url=="/client.html" or url=="/clientIE.html":
f=open('C:\\async\\'+url)
channel.send ('HTTP/1.1 200 OK\n\n'+f.read())
f.close()
channel.close()
elif '/messages' in url:
if req["method"]=='POST':
target=url[10:]
if target in connected:
connected[target].send("HTTP/1.1 200 OK\n\n"+req["content"])
print req["content"]+" sent to "+target
connected[target].close()
channel.close()
elif req["method"]=='GET':
user=url[10:]
connected[user]=channel
print user+' is connected'
HTML+Javascript中的client.html:
<html>
<head>
<script>
var user=''
function post(el) {
if (window.XMLHttpRequest) {
var text=el.value;
var req=new XMLHttpRequest();
el.value='';
var target=document.getElementById('to').value
}
else if (window.ActiveXObject) {
var text=el.content;
var req=new ActiveXObject("Microsoft.XMLHTTP");
el.content='';
}
else
return;
req.open('POST','messages/'+target,true)
req.send(text);
}
function get(u) {
if (user=='')
user=u.value
var req=new XMLHttpRequest()
req.open('GET','messages/'+user,true)
req.onload=function() {
var message=document.createElement('p');
message.innerHTML=req.responseText;
document.getElementById('messages').appendChild(message);
get(user);
}
req.send(null)
}
</script>
</head>
<body>
<span>From</span>
<input id="user"/>
<input type="button" value="sign in" onclick="get(document.getElementById('user'))"/>
<span>To</span>
<input id="to"/>
<span>:</span>
<input id="message"/>
<input type="button" value="post" onclick="post(document.getElementById('message'))"/>
<div id="messages">
</div>
</body>
</html>
3 个回答
非常感谢你,Florian,你的代码运行得很好!!!!
我重新使用了模板,并用我的COMET机制完善了主程序,效果好多了
Chrome和Firefox运行得非常顺利
但是IE在处理“长GET”请求时还是有问题
当它收到GET请求的响应时,还是不停地重复执行循环来打印消息。
我现在正在调查这个问题
这是我更新后的代码,适用于非常基础的JQuery+Python跨浏览器系统。
这个Python程序是基于Florian的代码写的:
bufsize = 4048
import socket
import re
from urlparse import urlparse
connected={}
class Headers(object):
def __init__(self, headers):
self.__dict__.update(headers)
def __getitem__(self, name):
return getattr(self, name)
def get(self, name, default=None):
return getattr(self, name, default)
class Request(object):
header_re = re.compile(r'([a-zA-Z-]+):? ([^\r]+)', re.M)
def __init__(self, sock):
header_off = -1
data = ''
while header_off == -1:
data += sock.recv(bufsize)
header_off = data.find('\r\n\r\n')
header_string = data[:header_off]
self.content = data[header_off+4:]
furl=header_string[header_string.index(' ')+1:]
self.url=furl[:furl.index(' ')]
lines = self.header_re.findall(header_string)
self.method, path = lines.pop(0)
path, protocol = path.split(' ')
self.headers = Headers(
(name.lower().replace('-', '_'), value)
for name, value in lines
)
if self.method in ['POST', 'PUT']:
content_length = int(self.headers.get('content_length', 0))
while len(self.content) < content_length:
self.content += sock.recv(bufsize)
self.query = urlparse(path)[4]
acceptor = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
acceptor.setsockopt(
socket.SOL_SOCKET,
socket.SO_REUSEADDR,
1,
)
acceptor.bind(('', 8007 ))
acceptor.listen(10)
if __name__ == '__main__':
while True:
sock, info = acceptor.accept()
request = Request(sock)
m=request.method
u=request.url[1:]
if m=='GET' and (u=='client.html' or u=='jquery.js'):
f=open('c:\\async\\'+u,'r')
sock.send('HTTP/1.1 200 OK\n\n'+f.read())
f.close()
sock.close()
elif 'messages' in u:
if m=='POST':
target=u[9:]
if target in connected:
connected[target].send("HTTP/1.1 200 OK\n\n"+request.content)
connected[target].close()
sock.close()
elif m=='GET':
user=u[9:]
connected[user]=sock
print user+' is connected'
还有经过压缩的HTML和Jquery:
<html>
<head>
<style>
input {width:80px;}
span {font-size:12px;}
button {font-size:10px;}
</style>
<script type="text/javascript" src='jquery.js'></script>
<script>
var user='';
function post(el) {$.post('messages/'+$('#to').val(),$('#message').val());}
function get(u) {
if (user=='') user=u.value
$.get('messages/'+user,function(data) { $("<p>"+data+"</p>").appendTo($('#messages'));get(user);});
}
</script>
</head>
<body>
<span>From</span><input id="user"/><button onclick="get(document.getElementById('user'))">log</button>
<span>To</span><input id="to"/>
<span>:</span><input id="message"/><button onclick="post()">post</button>
<div id="messages"></div>
</body>
</html>
我建议在客户端使用一个JS/Ajax库,这样可以避免你的代码在不同浏览器上出现问题。出于同样的原因,我还推荐使用像 SimpleHTTPServer 这样的Python HTTP服务器库,或者如果这个不够灵活的话,可以试试 Twisted 的一些东西。
还有一个主意是,使用像Wireshark这样的工具来检查浏览器发送了什么数据。
你遇到的问题是:
- 你的 TCP 套接字处理没有读取足够的数据
- 你的 HTTP 处理不完整
我推荐你看看以下的资料:
下面是一个可以处理 POST 请求的工作 HTTP 服务器的示例:
index = '''
<html>
<head>
</head>
<body>
<form action="/" method="POST">
<textarea name="foo"></textarea>
<button type="submit">post</button>
</form>
<h3>data posted</h3>
<div>
%s
</div>
</body>
</html>
'''
bufsize = 4048
import socket
import re
from urlparse import urlparse
class Headers(object):
def __init__(self, headers):
self.__dict__.update(headers)
def __getitem__(self, name):
return getattr(self, name)
def get(self, name, default=None):
return getattr(self, name, default)
class Request(object):
header_re = re.compile(r'([a-zA-Z-]+):? ([^\r]+)', re.M)
def __init__(self, sock):
header_off = -1
data = ''
while header_off == -1:
data += sock.recv(bufsize)
header_off = data.find('\r\n\r\n')
header_string = data[:header_off]
self.content = data[header_off+4:]
lines = self.header_re.findall(header_string)
self.method, path = lines.pop(0)
path, protocol = path.split(' ')
self.headers = Headers(
(name.lower().replace('-', '_'), value)
for name, value in lines
)
if self.method in ['POST', 'PUT']:
content_length = int(self.headers.get('content_length', 0))
while len(self.content) < content_length:
self.content += sock.recv(bufsize)
self.query = urlparse(path)[4]
acceptor = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
acceptor.setsockopt(
socket.SOL_SOCKET,
socket.SO_REUSEADDR,
1,
)
acceptor.bind(('', 2501 ))
acceptor.listen(10)
if __name__ == '__main__':
while True:
sock, info = acceptor.accept()
request = Request(sock)
sock.send('HTTP/1.1 200 OK\n\n' + (index % request.content) )
sock.close()