Recently I've been working on Chaquopy, Android-Python Library and my aim is to build a Port Scanner in android.
Code for Port Scanner Activity
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import com.chaquo.python.PyObject
import com.chaquo.python.Python
import com.chaquo.python.android.AndroidPlatform
import com.example.happlication.R
import com.example.happlication.adapter.servicesAdapter
import com.example.happlication.databinding.ActivityPortScannerBinding
import com.example.happlication.servicesModel
import java.util.*
class PortScannerActivity : AppCompatActivity()
{
lateinit var binding: ActivityPortScannerBinding
lateinit var activity: Activity
lateinit var scanoutput: PyObject
lateinit var pyObj: PyObject
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_port_scanner)
binding = DataBindingUtil.setContentView(this, R.layout.activity_port_scanner)
initView()
onClick()
}
private fun initView()
{
activity = this
window.statusBarColor = this.resources.getColor(R.color.black)
}
private fun onClick()
{
binding.ivBackArrow.setOnClickListener {
finish()
}
binding.btnSearch.setOnClickListener {
if (binding.edtIpaddress.text.toString().isNotEmpty())
{
if (binding.edtPortnumber.text.toString().isNotEmpty())
{
callScanner(this).execute()
binding.btnSearch.hideKeyboard()
}
else
{
Toast.makeText(activity, "Enter the port number", Toast.LENGTH_SHORT).show()
}
}
else
{
Toast.makeText(activity, "Enter the IP address", Toast.LENGTH_SHORT).show()
}
}
}
inner class callScanner(private var activity: PortScannerActivity?) : AsyncTask<String, String, String>()
{
override fun onPreExecute()
{
super.onPreExecute()
binding.loading.visibility = View.VISIBLE
}
var result=""
var error=""
override fun doInBackground(vararg p0: String?): String
{
try
{
if (! Python.isStarted()) {
Python.start(AndroidPlatform(this@PortScannerActivity))
}
var ipaddress=binding.edtIpaddress.text.toString()
var portnumber=binding.edtPortnumber.text.toString()
var py=Python.getInstance()
pyObj = py.getModule("portscanning")
scanoutput=pyObj.callAttr("portScanner",ipaddress, portnumber.toInt())
result=scanoutput.toString()
return result
}
catch (e: Exception)
{
error=e.toString()
}
return "false"
}
override fun onPostExecute(result: String?)
{
super.onPostExecute(result)
binding.loading.visibility = View.GONE
if (error.isNotEmpty())
{
binding.llErrorsection.visibility=View.VISIBLE
binding.tvError.visibility=View.VISIBLE
binding.tvError.text=error.toString()
}
else if (result.toString().isNotEmpty())
{
binding.llDetails.visibility=View.VISIBLE
binding.tvOuput.visibility=View.VISIBLE
binding.tvFinalOutput.visibility=View.VISIBLE
binding.tvFinalOutput.text=result.toString()
}
}
}
fun View.hideKeyboard() {
val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(windowToken, 0)
}
}
Python file: portscanning.py
# importing the sockets module
import socket
def portScanner(target, port):
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
t_ip=socket.gethostbyname(target)
print(f"starting scan on host {t_ip} with portnumber {port}")
try:
s.connect((t_ip, port))
a=(f"port {port} is open")
return a
except Exception as e:
b=(f"port {port} is closed")
print(e)
return b
Now if I run this Python file in normal windows-python interpreter then everything works fine
So is there some kind of permission I need to provide to this application, just like internet OR some thing like that ??
OR is it having some other solution, Please let me know Thanks for your answers!!