python 微信bot_我如何创建Python Bot自动登录到强制门户

本文介绍了如何使用Python和Selenium库创建一个自动化脚本,用于登录需要身份验证的强制门户,例如大学或组织提供的Wi-Fi网络。通过这个脚本,用户不再需要手动输入用户名和密码,只需点击即可完成登录。
摘要由CSDN通过智能技术生成

python 微信bot

by Ritvik Khanna

Ritvik Khanna着

我如何创建Python Bot自动登录到强制门户 (How I created a Python Bot to automatically log into a Captive Portal)

逐步Python教程,构建登录机器人 (A step by step Python tutorial to build a login bot)

Nowadays the Internet is not a privilege, it’s a necessity. Wherever we go, we require a constant connection to the Internet using either a Wi-Fi or mobile data based network.

如今,互联网已不再是一种特权,而是一种必要。 无论我们走到哪里,都需要使用Wi-Fi或基于移动数据的网络与Internet保持稳定连接。

Imagine we join a new university or an organisation, which provides us with Internet through Wi-Fi. The organisation might implement a very common login page for authentication of their users called a Captive Portal (also known as a Walled Garden).

想象一下,我们加入了新的大学或组织,该组织通过Wi-Fi为我们提供了Internet。 该组织可能会实施一个非常常见的登录页面来对其用户进行身份验证,称为Captive Portal (也称为围墙花园 )。

A Captive Portal is used for a number of reasons.

使用强制门户网站有多种原因。

  • They are used by universities and organisations to restrict the number of devices that are connected to the Wi-Fi network from one account/person.

    大学和组织使用它们来限制一个帐户/人连接到Wi-Fi网络的设备数量。
  • They are implemented to provide access to services that require authentication, payment, or other valid credentials that both the service provider and user agree to adhere by.

    实现它们是为了提供对需要身份验证,付款或服务提供商和用户均同意遵守的其他有效凭证的服务的访问。

There are a number of reasons and benefits to use a captive portal, but that’s not what this post is about.

使用强制门户网站有很多原因好处 ,但这不是本文的目的。

My company had implemented a captive portal that users needed to log into to be able to access the internet. Right after I connected to the wireless network, my browser used to open up the captive portal page and I needed to enter my username and password after which I was able to access the Internet. But there was a problem.

我公司已建立了一个强制门户,用户需要登录才能访问互联网。 连接到无线网络后,我的浏览器立即打开了强制门户网站页面,我需要输入用户名和密码,之后才能访问Internet。 但有一个问题。

Though implementing a Captive portal is good for protection against unauthorised Internet access, traffic identification and user management, it has a controlled connection to each device to ensure that all users have adequate access. A captive portal can,

尽管实施Captive门户可以很好地防止未经授权的Internet访问,流量识别和用户管理,但它具有与每个设备的受控连接以确保所有用户都具有适当的访问权限。 专属门户可以

  • Control the number of terminals per user

    控制每个用户的终端数量
  • Control the bandwidth consumption and/or per-session download speed

    控制带宽消耗和/或每个会话的下载速度
  • Restrict the type of traffic permitted and even specify the session timeout duration

    限制允许的流量类型,甚至指定会话超时时间

Due to these restrictions, if I put my system to sleep or stayed idle for longer than a few minutes, my system got logged out from the network. Therefore, after exceeding the idle time, I had to re-login.

由于这些限制,如果我使系统进入睡眠状态或空闲时间超过几分钟,则系统将从网络注销。 因此,超过空闲时间后,我必须重新登录。

Being a software developer who requires constant, on-the-go connectivity to the Internet on my system, I had to enter my username and password again and again which made it very cumbersome.

作为软件开发人员,需要在系统上与Internet保持持续不断的连接,我不得不一次又一次输入用户名和密码,这非常麻烦。

What if, after getting logged out, I could login into the captive portal with just a click of a button / icon on my system?

如果注销后,我只需在系统上单击一个按钮/图标就可以登录俘虏门户怎么办?

It should be doable! No need of entering a username and then a password that in most cases should be 8 characters long and should have at least one capital letter blah blah blah. Let’s see how we can do that.

应该可行的! 无需输入用户名,然后输入密码,密码在大多数情况下应为8个字符,并且至少应有一个大写字母等等。 让我们看看我们如何做到这一点。

实作 (Implementation)

Even if you are a novice in programming it should be pretty easy. I have coded this in the Python programming language. Python can be downloaded here, and we also require Selenium which can be downloaded here. You can also pip install selenium (recommended).

即使您是编程的新手,也应该很容易。 我已经用Python编程语言对此进行了编码。 Python可以在这里下载,我们也需要Selenium,可以在这里下载。 您还可以通过pip install selenium (推荐)。

Now let’s have a look at the code .

现在让我们看一下代码。

After importing the necessary libraries, we need to specify the following variables within the code.

导入必要的库后,我们需要在代码中指定以下变量。

Let me explain this now,

现在让我解释一下

  • website link is nothing but the login page link or the captive portal link against which a user would authenticate

    网站链接只不过是用户要对其进行身份验证的登录页面链接或强制门户链接

  • username and password is what you enter to login

    输入您的登录密码

  • element_for_username, element_for_password, element_for_submit are the names of the element in the HTML code of the login page

    element_for_username,element_for_password和element_for_submit是登录页面HTML代码中的元素名称

Let me show you how to find these.

让我告诉您如何找到这些。

As in the figure above,

如上图所示,

  • Open Inspect Element depending on your browser.

    根据您的浏览器打开检查元素

  • Search for the HTML element and copy the name of the input tag (in the above example its user_name).

    搜索HTML元素并复制输入标签的名称(在上面的示例中,其名称为user_name )。

  • Do the same to find out the HTML element for the password and submit button as well.

    进行同样的操作以找到密码HTML元素并提交按钮

  • These strings will be the value for your element_for_username, element_for_password, element_for_submit.

    这些字符串将是您的element_for_username,element_for_password和element_for_submit的值。

Most of the work is done!

大部分工作都完成了!

Note: I work on macOS so I’ll be implementing the bot for Safari. For Chrome and others, use Chrome via chromedriver.

注意:我在macOS上工作,因此我将为Safari实现该机器人。 对于Chrome和其他浏览器,请通过chromedriver使用Chrome。

Copy this code with the rest of the code and save it as a Python (.py file). Run the file using python script.py. You will see the browser automatically opens up the login page and enters the details and submits it. You don’t even have to type the username and password anymore. Isn’t it cool?

将此代码与其余代码复制并保存为Python(.py文件)。 使用python script.py运行文件 您将看到浏览器自动打开登录页面,然后输入详细信息并提交。 您甚至不必再输入用户名和密码。 是不是很酷?

Note: The complete code is available on GitHub.

注意:完整的代码可在GitHub找到

在macOS中使用Automator创建应用程序(可选) (Using Automator in macOS for creating an Application (Optional))

In macOS you can create an application that can follow a specific set of workflows for any task which is done repeatedly.

在macOS中,您可以创建一个应用程序,该应用程序可以遵循一组重复执行的任务的特定工作流。

In this section I’ll explain how to make the above Python script into a automator app. This will enable the user to log into the captive portal and simply click the automator app file.

在本节中,我将说明如何将上述Python脚本制作为自动化应用程序。 这将使用户能够登录到强制门户并只需单击automator应用程序文件。

Now let’s look at the steps for implementing this:

现在,让我们看一下实现此步骤:

Step 1: Open Automator. Create a new service or File > New > Service

第1步:打开Automator 。 创建一个新服务File &g t;>服务

Step 2: Add a “Run Shell Script” action, setting Shell: to /bin/bash and Pass input: to as arguments.

第2步:添加一个“ 运行Shell脚本 ”操作,将Shell:设置为/ bin / bash,并将input:设置为arguments

Step 3: Now add the bash code as under. Save the file on the desktop.

步骤3:现在,按如下所示添加bash代码。 将文件保存在桌面上。

Step 4: Click the file and Voilà!

步骤4:点击文件,然后点击!

Finding a solution to a problem as simple as typing something like login credentials repeatedly can be done easily. Once you know what the problem statement is, finding the solution is easier. I hope this post gave you an idea of how Selenium and Python works and how creating a Python bot is easy.

找到问题的解决方案就像重复输入登录凭据之类的简单操作很容易。 一旦知道问题陈述是什么,查找解决方案就变得容易了。 我希望本文能使您了解Selenium和Python的工作原理以及创建Python机器人的过程很容易。

翻译自: https://www.freecodecamp.org/news/how-i-created-a-python-bot-to-automatically-log-into-a-captive-portal-3d4ba04dee9f/

python 微信bot

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值