Ruby/RoR 和许多子进程
我正在尝试使用 ruby/rails 构建一个免费的网络应用程序它应该能够通过各种移动运营商的在线表格发送短信。 (例如这个(俄语))。
因此,我需要
- 等待想要通过我的网站发送短信的用户。
- 建立与运营商网站的连接。可能是使用机械化。
- 检索验证码
- 向用户显示验证码
- 允许用户输入消息并
- 在运营商网站上提交验证码表单(包含消息、验证码、电话号码)
在整个过程中,与运营商网站的连接应处于活动状态(否则验证码将发生变化)。据我了解,每次发送短信时我都需要创建一个(子)进程。 您能告诉我在rails\ruby中处理这个问题的最佳方法是什么吗?
我对网络开发还很陌生...... 我应该使用线程吗?叉子?波彭?使用 PTY?一些外部宝石?我应该如何与我的流程进行通信?
I am trying to build a free web application using ruby/rails It should be able to send sms through online forms of various mobile operators. (like this one (in russian)).
So, I need to
- wait for the user, who wants to send an sms through my website.
- establish connection to operator website. Probably, using Mechanize.
- retrieve captcha
- show captcha to the user
- allow user to enter a message and captcha
- submit form on operators website (with message, captcha, phone number)
The connection to the operator website should be alive during all this process (otherwise captcha will change). As far as I understand, I need to create a (sub)process each time sms is sent.
Could you please advise what is the best way of handling this in rails\ruby?
I am still rather new to web-development...
Should I use threads? forks? popen? using PTY? some external gem? How should I communicate with my process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设运营商的网站没有什么特别之处,不,您不需要在整个过程中保持连接处于活动状态。一般来说,网页上的表单的工作方式如下:您访问 URL,您的网络浏览器会下载包含表单的页面。在你的情况下,它还会有一个 。标签或类似的内容来显示验证码。一旦您的浏览器下载了该页面,连接就会被切断。填写表单并单击“提交”后,您的 Web 浏览器将打开与服务器的新连接并发送数据,服务器发送其响应(无论您单击“提交”后显示什么页面)。
您的程序所要做的就是模仿这种体验。所以: 1)下载带有表格的页面。抓取表单字段(确保您不会错过任何隐藏字段 - 可能会有一些带有验证码的隐藏字段)和验证码。 2) 构建一个页面来向您的用户显示,其中包含验证码和包含他们需要填写的所有字段的表单。如果原始表单中存在隐藏字段,请确保也包含它们的值(作为表单中的隐藏字段),因为当用户提交表单时,您将需要它们。 3) 然后,当用户提交表单时,将数据(包括隐藏值和用户为验证码输入的内容)发送给操作员。 4) 最后,检查操作员是否指示成功,并构建一个页面来告诉您的用户。
如果您在 Rails 中执行此操作,您的控制器中可能有两种方法:一种称为“show”(上面的步骤 1 和 2),它将从运营商的站点中抓取验证码和其他信息并向用户显示您的信息。表单视图,以及表单将提交到的名为“发送”(上面的步骤 3 和 4)的视图,该视图将获取数据并将其发送到运营商的网站,收集响应并告诉用户是否成功或不。
注意:在处理这些问题之前,您需要先阅读运营商的服务条款。我相当确定这种事情会违反他们的 TOS,如果他们注意到您的服务器以他们的方式发送大量请求,他们会很快阻止您。
Assuming there's nothing special about the operator's web site, no, you don't need to keep a connection alive during the whole process. Generally speaking, forms on web pages work like this: You visit the URL, your web browser downloads the page with the form on it. In your case, it will also have an <img> tag or similar to show the CAPTCHA. Once your browser has downloaded the page, the connection is severed. After you fill out the form and click on Submit, your web browser opens a new connection to the server and sends the data, and the server sends its response (whatever page is shown after you click Submit).
All your program has to do is emulate this experience. So: 1) Download the page with the form on it. Scrape the form fields (make sure you don't miss any hidden fields--with a CAPTCHA there will probably be some) and the CAPTCHA. 2) Build a page to show your user that includes the CAPTCHA and a form with all the fields they need to fill out. If there were hidden fields in the original form, make sure you include their values (as hidden fields in your form) as well, because when the user submits your form you'll need them. 3) Then, when the user submits your form, send the data, including the hidden values and what the user entered for the CAPTCHA, to the operator. 4) Finally, check if the operator indicated success, and build a page to tell your user.
If you're doing this in Rails, you'll probably have two methods in your controller: One called e.g. 'show' (steps 1 and 2 above) that will scrape the CAPTCHA and other info from the operator's site and show the user your form view, and one called e.g. 'send' (step 3 and 4 above) that the form will submit to, and which will take their data and send it to the operator's web site, collect the response and tell your user if it was successful or not.
Note: You'll want to read the operators' terms of service before you bother with any of this. I'm fairly certain that this kind of thing will be against their TOSes and if they notice your server sending a lot of requests their way they're going to block you pretty quick.
为了回答您的另一个问题,您可以使用 DRb 或 background_job (又名 BJ)在后台实际完成发送,这样您的用户提交验证码后就不必等待响应。或者您可以将其包装在 ajax 中,并让 DRb/BJ 进程在短信发送发生时通知您,以便您可以通知用户成功或任何问题。
通常要避免在 Ruby 中打开线程,因为有很多很棒的 gem 可以满足我们的需要。并不是说您不应该使用线程,只是在大多数情况下它可能已经做得很好了。
To answer another question of yours, you can use DRb or background_job (aka BJ) to actually accomplish the sending in the background so that after your user submits the captcha they don't have to wait for the response. Or you could wrap this in ajax and have the DRb/BJ process notify you when the sms sending has happened so you can notify the user of success or any problems.
Typically opening threads in Ruby is something to avoid as there are so many great gems that do what we need. Not to say that you shouldn't use threads, just that for the most part it's probably already been done really well.