<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Townes &#187; 无法及时自动同步</title>
	<atom:link href="https://thetownes.coolpage.biz/?feed=rss2&#038;tag=%E6%97%A0%E6%B3%95%E5%8F%8A%E6%97%B6%E8%87%AA%E5%8A%A8%E5%90%8C%E6%AD%A5" rel="self" type="application/rss+xml" />
	<link>https://thetownes.coolpage.biz</link>
	<description>本站已转移至：http://www.thetownes.info</description>
	<lastBuildDate>Thu, 01 Aug 2013 11:49:56 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.2</generator>
		<item>
		<title>解决Dropbox中国无法及时自动同步的问题</title>
		<link>https://thetownes.coolpage.biz/?p=195</link>
		<comments>https://thetownes.coolpage.biz/?p=195#comments</comments>
		<pubDate>Thu, 11 Jul 2013 15:02:36 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[云存储]]></category>
		<category><![CDATA[日常故障排除]]></category>
		<category><![CDATA[无法及时自动同步]]></category>

		<guid isPermaLink="false">http://thetownes.coolpage.biz/?p=195</guid>
		<description><![CDATA[最近由于想要使用Dropbox的多人协作功能，就发现Dropbox不能自动同步其 &#8230; <a href="https://thetownes.coolpage.biz/?p=195">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>最近由于想要使用<a href="http://www.williamlong.info/archives/2044.html" target="_blank">Dropbox</a>的多人协作功能，就发现Dropbox不能自动同步其他机器上产生的文件变化，经过一番搜索，发现原来是GFW在作怪（GFW和GD的性质和用心我们心知肚明，就不在这里评价了）。月光博客发布了<a href="http://www.williamlong.info/archives/2585.html" target="_blank">解决Dropbox无法实时更新的问题</a>分析了产生这个问题的原因并提出一个有效的解决方案。但是在使用时我发现，我找不到一个优良稳定的代理服务器，也没工夫去学习privoxy软件的配置和使用，而且我要将解决方案提供给我的合伙人，一个复杂的方案是不能接受的。经过一番研究，提出如下比较简单的办法。</p>
<h3>　　分析</h3>
<p>我发现Dropbox向notify8发出的请求很简单，回应也很简单，一共有两种：<code>{"ret":"new"}和{"ret":"punt"}</code></p>
<p>分别表示云端有变化和无变化，然后客户端考虑去下载文件列表并同步。</p>
<p>经过一番痛苦的失败，我发现这个请求的其实是一个comet请求，服务器端并不马上回应，而是会挂起，如果有变化，则马上回应，如果一直没有变化，大约一分钟超时回应punt，然后客户端再连接服务器。在我分析Dropbox的过程中一直不解：为什么Dropbox的其他请求都是https，而只有这一个请求是http的。现在找到了答案：因为它是comet请求，长连接，而且连接频率非常高，如果使用https代价太大，而且影响效率。如果这个请求返回new，客户端就会使用https连接服务器端。</p>
<h3>　　解决</h3>
<p>由此提出一个完美的解决方案，不仅可以解决本机的问题，而且可以解决朋友的问题，只要让朋友修改hosts为我的ip地址：</p>
<ul>
<li>修改hosts将notify8对应的ip地址改为本机</li>
<li>在本机建立一个http服务，代理notify8得到dropbox的返回值，再原封不动地返回给本机dropbox客户端</li>
</ul>
<p>具体方法是使用tornado，进行一步http请求，这样只占用很少一部分系统资源。贴出代码。</p>
<h3>　　代码</h3>
<p><code><br />
import tornado.httpserver<br />
import tornado.ioloop<br />
import tornado.options<br />
import tornado.web<br />
from tornado.options import define, options<br />
from tornado import httpclient<br />
define("port", default=8888, help="run on the given port", type=int)<br />
class Application(tornado.web.Application):<br />
def __init__(self):<br />
handlers = [<br />
(r"/subscribe", NotifyHandler),<br />
(r"/.*", HomeHandler),<br />
]<br />
settings = dict(<br />
debug=True,<br />
)<br />
self.debug = True<br />
tornado.web.Application.__init__(self, handlers, **settings)<br />
class HomeHandler(tornado.web.RequestHandler):<br />
def get(self):<br />
self.set_header("Content-Type", "text/plain")<br />
self.write("Hello from Tornado!")<br />
class NotifyHandler(tornado.web.RequestHandler):<br />
@tornado.web.asynchronous<br />
def get(self):<br />
self.set_header("Content-Type", "text/plain")<br />
url = xxxxx #关键代码还是不贴出来了，人怕出名猪怕壮，要是大多数人会用了，估计这个方法死期不远矣!<br />
http_client = httpclient.AsyncHTTPClient()<br />
http_client.fetch(url, self.handle_response, request_timeout=100.0)</code></p>
<p>def handle_response(self, response):<br />
if response.error:<br />
print &#8220;Can not connect.&#8221;<br />
self.write(&#8220;{&#8220;ret&#8221;: &#8220;new&#8221;}&#8221;)<br />
else:<br />
print &#8220;Connect Successfull.&#8221;<br />
self.write(response.body)<br />
self.finish()<br />
def main():<br />
tornado.options.parse_command_line()<br />
http_server = tornado.httpserver.HTTPServer(Application())<br />
http_server.listen(options.port)<br />
tornado.ioloop.IOLoop.instance().start()</p>
<p><code>if __name__ == "__main__":<br />
main()</code></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://thetownes.coolpage.biz/?feed=rss2&#038;p=195</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
