作者guestttttt (长常久九)
看板Python
标题[问题] http传输速度
时间Mon Oct 2 23:07:12 2017
请问各位高手:
我在写一个图片辨认的程式(b程式),搭配既有的a程式
两个程式是用http传输,程式流程为
1. a程式抓图,传图片资料给b程式
2. b程式辨认图片,传辨认结果给a程式
在下面四种条件下,我测量a程式发出资料到接收到结果的时间,程式码都没改
当a程式 在Windows 7电脑,b程式 在Windows 7电脑,约为0.3秒
当a程式 在Windows 7电脑, b程式 在Ubuntu 16.03电脑,也是0.3秒
当a和b程式在同一台电脑,OS是Windows 7,约为1.3秒
当a和b程式在同一台电脑,OS是Ubuntu 16.03,约为0.2秒
请问为什麽a和b程式在同一台电脑,OS是Windows 7时,整体时间会特别慢?
我的程式码如下
--------------a程式--------------
import numpy as np
from PIL import Image
import requests
...
imgs0 = [Image.open(fileee) for fileee in imagefiles]
imgs0 = np.array([np.array(resize(img0, r_shape))
for img0 in imgs0])
start = time.time()
res = requests.post("
http://localhost:8001", data=imgs0.tobytes())
#res = requests.post("
http://192.168.1.150:8001", data=imgs0.tobytes())
### client and server at different PC
print("{:.3f}".format(time.time() - start))
...
--------------b程式--------------
import http.server
import numpy as np
...
class MyHandler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
content_len = int(self.headers.get('content-length', 0))
post_body = self.rfile.read(content_len)
imgs = np.frombuffer(post_body, dtype=np.uint8)
imgs = imgs.reshape((-1,) + shape + (3,))
scores = model.predict(imgs)[:, 1].tobytes()
self.send_response(200)
self.end_headers()
self.wfile.write(scores)
server = http.server.HTTPServer(('127.0.0.1', 8001), MyHandler)
#server = http.server.HTTPServer(('192.168.1.150', 8001), MyHandler)
### client and server at different PC
server.serve_forever()
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 219.84.255.71
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1506956846.A.E46.html
1F:推 ckc1ark: 把localhost换成127.0.0.1试试 10/03 00:11
谢谢ckclark大大,改了有效,时间也减少为0.3秒左右
我查一下传输变慢的原因,参考这篇文章
https://read01.com/zh-tw/nmB5Mz.html#.WdNG-VuCyUk
是因为我将server(b程式)设定为127.0.0.1,client(a程式)设为localhost
当client呼叫server时,需将locahost 转为127.0.0.1,导致特别慢吗?
为什麽在Ubuntu 下没有这个问题?
2F:推 f496328mm: 借问一下 两台电脑传输 是不是都要固定 IP? 10/03 00:50
3F:→ f496328mm: 我也想做两台电脑传输 可是只有一台固定 IP 可以对外 10/03 00:51
4F:→ f496328mm: 利用 py 彼此传输的概念是 HTTPServer 10/03 00:57
5F:→ f496328mm: 架设临时的伺服器吗? 然後再用另一台去抓? 10/03 00:58
6F:→ f496328mm: 所以即使没有固定 IP, 也没关系? 10/03 00:58
我的方法如同lc85301大大所说
两台电脑用网路跳线连接
一台当server,一台当client
IP分别是192.168.1.x 和192.168.1.y
Gateway 192.168.1.1
Mask 255.255.255.0
不过这两台电脑都不对外,要连接外部网路要另外设定
7F:→ s860134: 对接 其中一台当 gateway 就好 10/03 07:26
8F:→ s860134: 没有 IP 就只能用广播的方式 10/03 07:27
9F:推 lc85301: 是要做到像这样? 10/03 11:14
※ 编辑: guestttttt (219.84.255.71), 10/03/2017 23:41:01
11F:→ darkgerm: 没对外的话其实 gateway 也不用设 10/04 11:14
12F:→ s860134: 楼上是对的 10/14 04:41