How to make android app as server and python as client side - python

Description
So basically i am building an android app in which i am using kotlin and which will behave as server.Whilst on the client side i am using python.I am using sockets for this purpose.I have to communicate using UDP.But i am unable to make connection to my android app.In python script sock.connect(('10.0.2.2', 6000)) i have also tried placing the emulator ip instead of localhost.I just want to send and receive simple messages.
Another Issue: Python script just times out it does not even gets into the loop.
Server.kt
package com.example.soundsource
import android.content.Intent
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import java.io.BufferedInputStream
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.PrintWriter
import java.lang.Exception
import java.lang.ref.WeakReference
import java.net.ServerSocket
import java.net.Socket
class MainActivity : AppCompatActivity() {
private lateinit var textView:TextView
private var message = " "
private lateinit var client:Socket
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val sendButton:Button = findViewById(R.id.send_button)
val showLocation = findViewById(R.id.show_location) as? Button
showLocation?.setOnClickListener {
val intent = Intent(this,SoundLocation::class.java)
startActivity(intent)
}
textView = findViewById(R.id.text_view)
sendButton.setOnClickListener{
t.start()
}
}
private val t = Thread(Runnable {
val port = 5000
val server = ServerSocket(port)
this.message = "Message from client"
this#MainActivity.runOnUiThread {
this.textView.text = server.localPort.toString()
}
val i = InputStreamReader(this.client.getInputStream())
val b = BufferedReader(i)
while (true){
this.client = server.accept()
message += " "+ this.client.localAddress.toString()+b.readLine()
this#MainActivity.runOnUiThread{
this.textView.text = server.localSocketAddress.toString()
}
t2.start()
}
})
private val t2 = Thread(Runnable {
val p = PrintWriter(this.client.getOutputStream())
p.println("sending message back")
runOnUiThread {
this.textView.text = "message sent...."
}
})
}
Client.py
import socket
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('10.0.2.2', 5000))
while True:
print("you are about to.....")
data = sock.recv(1024)
print('you received :', data)
sock.sendall(bytes("hey kotlin....", 'utf-8'))
main()

Related

Socket doesn't communicate with other networks

Im trying to send a 8 digit pin code from one computer to another, the code works when both of the computers are on the same networks.But there seems to be a problem when they are not on the same network. it just doesnt send the pin to the server pc.
receives the data from the client and saves it
server.py
import socket
import json
import Classes
def recive_pin(s_data):
pin = s_data.recv(int(s_data.recv(1).decode())).decode()
return pin
def main():
# gets port from json file
with open("Config.JSON", 'r') as f:
json_data = json.load(f)
port = json_data["load"]["port"]
# create the tcp socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# binds the socket to localhost , config port number
ip = '0.0.0.0'
s.bind((ip, port))
user_data = {}
while 1:
s.listen()
s_data, address = s.accept()
command = s_data.recv(1).decode()
if command == "P":
user_data[recive_pin(s_data)] = address
print(user_data)
if __name__ == '__main__':
main()
client.py
create a 8 digit code and sends it to the server
import socket
import json
import Classes
def main():
# gets the user input controlled/controller.
with open("Config.JSON", 'r') as f:
json_data = json.load(f)
port = json_data["load"]["port"]
ip = json_data["load"]["ip"]
c = Classes.handler(ip, port)
x = Classes.pin.create_pin()
c.send_pin(x)
if __name__ == '__main__':
main()
Classes.py
import random
import string
import socket
class handler:
def __init__(self, comunication_ip=0, port=0, ):
self.__comunication_ip = comunication_ip
self.__port = int(port)
# type of command to send to the server
# tuple of the server details
self.__server_details = (self.__comunication_ip, self.__port)
def send_pin(self, verification_pin):
# command type : Send_pin("P")
command_type = "P"
# creates a socket to send the pin to the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# adds the type of the command and the length of the pin to the Pin.
msg = command_type + str(len(verification_pin)) + verification_pin
s.connect(self.__server_details)
s.send(msg.encode())
class pin:
#staticmethod
# def __init__(self, defult_pin=0):
# self.__pin = defult_pin
def create_pin():
characters = string.ascii_letters + string.digits + string.punctuation
verification_pin = str(''.join(random.choice(characters) for length in range(8)))
return str(verification_pin)
i also use a Json file to save the port number and the server ip :
{
"load":{
"ip":"10.100.102.94",
"port": 12000
}
}

grpc server memory leak when using string tensor in tensorflow?

Ubutun 18, python 3.6, gRPC 1.24.3
server.py:
import time
import grpc
from concurrent import futures
import tensorflow as tf
from google.protobuf import any_pb2
import numpy as np
import random
import compute_pb2, compute_pb2_grpc
class ComputeServicer(compute_pb2_grpc.ComputeServicer):
def SayHello(self, request, ctx):
max_len = str(len(request.helloworld))
return compute_pb2.HelloReply(result=max_len)
def Compute(self, request, ctx):
n = 100
# This 2 lines makes memory leak
for i in range(n):
value = tf.constant([str(random.randint(10000000,99999999)) * 16] * 5000000, dtype=tf.string)
'''
for i in range(n):
value = tf.constant([i] * 100000000, dtype=tf.int64)
'''
value = tf.constant([1] * 50000000, dtype=tf.int64)
tensor_proto = tf.make_tensor_proto(value)
any_pb = any_pb2.Any()
any_pb.Pack(tensor_proto)
value_proto = compute_pb2.Value(tensor=any_pb)
return compute_pb2.ComputeResponse(value=value_proto)
def main():
options = [
('grpc.max_send_message_length', -1), ('grpc.max_receive_message_length', -1)
]
server_kwargs = {}
server_kwargs['options'] = options
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10), **server_kwargs)
servicer = ComputeServicer()
compute_pb2_grpc.add_ComputeServicer_to_server(servicer, server)
server.add_insecure_port('127.0.0.1:19999')
server.start()
try:
print("running...")
time.sleep(1000)
except KeyboardInterrupt:
print("stopping...")
server.stop(0)
if __name__ == '__main__':
main()
client.py
import grpc
import compute_pb2
import compute_pb2_grpc
import tensorflow as tf
import time
_HOST = '127.0.0.1'
_PORT = '19999'
def main():
num = 0
while num < 100:
with grpc.insecure_channel("{0}:{1}".format(_HOST, _PORT),
options=[('grpc.max_send_message_length', -1),
('grpc.max_receive_message_length', -1)]
) as channel:
client = compute_pb2_grpc.ComputeStub(channel=channel)
response = client.Compute(compute_pb2.HelloRequest(helloworld="123456"))
value_proto = response.value
tensor_proto = tf.make_tensor_proto(values=0)
if not value_proto.tensor.Unpack(tensor_proto):
raise ValueError('Unable to unpack the received tensor value.')
tensor_value = tf.make_ndarray(tensor_proto)
print(num)
num+=1
if __name__ == '__main__':
main()
compute.proto
syntax = "proto3";
import "google/protobuf/any.proto";
package compute;
service Compute {
rpc SayHello (HelloRequest) returns (HelloReply) {}
rpc Compute(HelloRequest) returns (ComputeResponse) {}
}
message HelloRequest {
string helloworld = 1;
}
message HelloReply {
string result = 1;
}
message ComputeResponse {
Value value = 1;
}
message Value {
oneof value {
google.protobuf.Any tensor = 1;
}
}
python -m grpc_tools.protoc -I ./ --python_out=./ --grpc_python_out=./
compute.proto
python server.py
python client.py
When run commands above, The memory usage increases over time in server side. If I remove string tensor in server or replace it with int64(see my annotation), The server's memory usage is stable then.
Thanks in advance.

i want to receive string from python server

I'm making an application using android and Python.
android is client
python is server
i send image file to python and want to receive string from server
but No strings are coming from the server.
socketIn.readLine() This part does not work.
try { // 소켓을 생성하고 입출력 스트립을 소켓에 연결
clientSocket = Socket(ip , port)
Log.d("Socket>>>>>>", "ip and port open Success!!!!!")
//val inputStream = clientSocket.getInputStream()
val tempfile = file
try{
socketIn = BufferedReader(InputStreamReader(clientSocket.getInputStream(), "UTF-8"))
//socketOut = PrintWriter(BufferedWriter(OutputStreamWriter(clientSocket.getOutputStream())),true)
dis = DataInputStream(FileInputStream(tempfile))
dos = DataOutputStream(clientSocket.getOutputStream())
val buf = ByteArray(1024)
var read_length : Int = 0
do {
read_length = dis.read(buf)
if(read_length == -1)
break
dos.write(buf)
dos.flush()
} while(read_length > 0)
var line : String?
var StringBuilder = StringBuilder()
do {
line = socketIn.readLine()
if(line == null)
break
StringBuilder.append(line)
}while(line != null)
onApiResult(line)
} catch (e : Exception){
Log.d("error", "${e}")
onApiFailed()
} finally {
clientSocket.close()
}
this is my android client code. client send the image to python server using tcp.
The image is sent well but the string does not come.
There is an error here line = socketIn.readLine()
please tell me how to fix it
from socket import *
serverPort = 8000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('123.234.345.456', serverPort))
serverSocket.listen(1)
print('The server is ready to receive')
msg = "hi"
while True:
connectionSocket, addr = serverSocket.accept()
img_file = open('hi.jpg', "wb")
while True:
sentence = connectionSocket.recv(1024)
data = sentence
img_file.write(sentence)
if sentence:
print("recving IMg....")
print(sentence)
sentence = connectionSocket.recv(1024)
img_file.write(sentence)
else:
print('Done')
img_file.close()
break
connectionSocket.sendall(bytes(msg, 'UTF-8'))
connectionSocket.close()
Just a guess -- you are sending binary data, but your client code uses java Reader/Writer classes (dedicated to reading text-like data). Use Streams, instead of Reader/Writer. There is no notion of 'endOfLine' when reading binary data. Also note, that client call to 'readLine()' assumes client platform dependent end-of-line, whatever it may be. If server and client platform differ, it will never work.

How to implement pub/sub socket.io client in python?

I've been trying to implement the python equivalent of the code below but cannot seem to get it working.
var io = require('socket.io-client')
var socket = io('http://devel.hz.udoidio.info:5000')
socket.on('connect', function () {
socket.emit('subscribe', 'new-tx')
})
socket.on('new-tx', function (txid) {
console.log('New tx: ' + txid)
})
I tried this approach but it does not seem to yield anything.
from socketIO_client import SocketIO, LoggingNamespace
def on_response(*args):
print 'on_response', args
baseurl = "http://v1.livenet.bitcoin.chromanode.net"
socketIO = SocketIO(baseurl, 80, LoggingNamespace)
socketIO.on('subscribe', on_response)
socketIO.emit('subscribe', 'new-block')
socketIO.wait()
I resloved the problem, below is the correct solution.
from socketIO_client import SocketIO
def on_response(*args):
print 'on_response', args
baseurl = "http://devel.hz.udoidio.info"
with SocketIO(baseurl, 5000) as socketIO:
socketIO.on('new-tx', on_response)
socketIO.emit('subscribe', 'new-tx')
socketIO.wait()

How to asynchronously read data via modbus/TCP and send them to web

I need to receive data from device connected via Ethernet (modbus/TCP) and send it to webpage (maybe using web sockets).
I can't find good examples. Now I can connect with driver and print values using ModbusClientProtocol.read_input_registers() but I had to create own factory and protocol class. I am using autobahn, twisted, pymodbus.
I've no familiarity with modbus or pymodbus, so I'm guessing and leaving a lot of blanks for you to fill in.
This is hacked out of something I recently put together to receive snmptraps and redistribute the information to connected websockets.
Hopefully this is enough to get you going:
#!/usr/bin/python
from twisted.internet import protocol, reactor, utils, defer
from twisted.web.server import Site
from twisted.web.static import File
from autobahn.websocket import WebSocketServerFactory, WebSocketServerProtocol
from autobahn.util import newid
from autobahn.resource import WebSocketResource
class ModbusThing(object):
def __init__(self,clientAddress):
self.clientAddress = clientAddress
self.client = None
def start(self):
pass
## Create client connection to modbus server
## Start Looping Call of pollForData with suitable interval
def pollForData(self):
pass
## Call read methods on ModbusClient object, add call backs to process the results
## Add errorBacks to notify of errors
def resultCallback(self,result):
pass
## Process the data from a read request
## Assumes that your websocket clients expect json like {"event":"update","data":[0,1,2]}
message = dict(event="update",data=processedResults)
self.broadcast(json.dumps(message))
def broadcast(self,msg):
"""Override me"""
pass
class TrackingWebSocketProtocol(WebSocketServerProtocol):
def onOpen(self):
self.session_id = newid()
self.factory._addSession(self,self.session_id)
print "Socket Open %s" % (self.peerstr,)
def onMessage(self,payload,isBinary):
print "Message received from %s\n\t: %r" % (self.peerstr,payload)
def onClose(self,wasClean,code,reason):
self.factory._removeSession(self)
print "Socket Closed %s" % (self.peerstr,)
class TrackingWebSocketFactory(WebSocketServerFactory):
def __init__(self,*args,**kwargs):
WebSocketServerFactory.__init__(self,*args,**kwargs)
self.proto2session = {}
self.session2proto = {}
def _addSession(self,proto,session_id):
if not self.proto2session.has_key(proto):
self.proto2session[proto] = session_id
else:
raise Exception("logic error - dublicate _addSession for protoToSessions")
if not self.session2proto.has_key(session_id):
self.session2proto[session_id] = proto
else:
raise Exception("logic error - dublicate _addSession for sessionsToProto")
def _removeSession(self,proto):
if proto in self.proto2session:
session_id = self.proto2session[proto]
del self.proto2session[proto]
if session_id in self.session2proto:
del self.session2proto[session_id]
def sendToAll(self,message,binary=False):
prepped = self.prepareMessage(message,binary)
for proto in self.proto2session.keys():
proto.sendPreparedMessage(prepped)
def run():
## WebSocket Factory
wsfactory = TrackingWebSocketFactory('ws://yourhostname:80')
wsfactory.protocol = TrackingWebSocketProtocol
wsresource = WebSocketResource(wsfactory)
## Modbus handler
modbus_thing = ModbusThing((addressofserver,portofserver))
modbus_thing.broadcast = wsfactory.sendToAll
modbus_thing.start()
## WebServer Site
# "static" subdirectory, containing http served resources, e.g. index.html, javascript and css
root = File("static")
# Your websocket service as 'ws://yourhostname/ws'
root.putChild("ws", wsresource)
site = Site(root)
reactor.listenTCP(80,site)
def main():
reactor.callWhenRunning(run)
reactor.run()
if __name__=='__main__':
main()
On the browser side of things. A little module for interacting with websockets is handy:
var FancyWebSocket = function(url){
var conn = null;
var fws = this;
if ("WebSocket" in window) {
conn = new WebSocket(url);
} else if ("MozWebSocket" in window) {
conn = new MozWebSocket(url);
} else {
console.log("Error Websockets not supported in browser");
return;
}
var callbacks = {};
var debug = true;
this.bind = function(event_name, callback){
callbacks[event_name] = callbacks[event_name] || [];
callbacks[event_name].push(callback);
return this;// chainable
};
this.send = function(event_name, event_data){
var payload = JSON.stringify({event:event_name, data: event_data});
conn.send( payload ); // <= send JSON data to socket server
return this;
};
this.close = function(){ conn.close(); return this;}
// dispatch to the right handlers
conn.onmessage = function(evt){
if (debug) console.log("Websocket(" + conn.URL + ") Message: " + evt.data)
var json = JSON.parse(evt.data)
dispatch(json.event, json.data)
};
conn.onclose = function(){
if (debug) console.log("Websocket(" + conn.URL + ") Closed");
dispatch('close',fws);
}
conn.onopen = function(){
if (debug) console.log("Websocket(" + conn.URL + ") Open");
dispatch('open',fws);
}
conn.onerror = function(e){
if (debug) console.log("Websocket(" + conn.URL + ") Error: " + error);
dispatch('error',fws,e);
}
this.setdebug = function(v) { debug=v; return this; }
var dispatch = function(event_name, message){
var chain = callbacks[event_name];
if(typeof chain == 'undefined') return; // no callbacks for this event
for(var i = 0; i < chain.length; i++){
chain[i]( message )
}
}
};
Then in your browser console:
conn = new FancyWebSocket("ws://yourhostname/ws");

Categories

Resources