Python server and java client not working properly - python

I want to make a socket between Java client
`
import java.util.*;
import java.net.*;
import java.io.*;
public class Main
{
public static void client()
{
final int port = 2003;
final String host = "localhost";
try
{
Socket soc = new Socket(host, port);
if(soc.isBound()){
System.out.println("socket binded");
}else if(soc.isConnected()){
Scanner in = new Scanner(soc.getInputStream());
System.out.println("enter msg");
String x = in.nextLine();
PrintStream p = new PrintStream(soc.getOutputStream());
p.println("#client" + x);
} else if(soc.isClosed()){
System.out.println("Closed tunnel");
System.exit(0);
soc.close();
}
}
catch (Exception e)
{
}
}
public static void main(String[] args)
{
client();
}
}
`
Its having problem allowing me to use the scanner to send msg to Python server{I dunno how to make this}
PS : am new and dunno how to make this snippets formatted

Related

How can I set the file descriptors for a new Process in Haxe to use it with a socket?

I am translating some code to Haxe from Python so that I can target more platforms. But I'm having trouble with the following snippet.
import socket
from subprocess import Popen
host='127.0.0.1'
port=8080
file='handle.sh'
handler = socket.socket()
handler.bind((host, port))
handler.listen(5)
conn, address = handler.accept() # Wait for something to connect to the socket
proc = Popen(['bash', file], stdout=conn.makefile('wb'), stdin=conn.makefile('rb'))
proc.wait()
conn.shutdown(socket.SHUT_RDWR)
conn.close()
In Python, I can set stdin and stdout to the relevant file descriptors of the socket. But by the time I call shutdown, all the data to be sent is in the right buffer and nothing blocks me.
But I can't do this in Haxe as far as I can tell because input and output from the socket and, stdin and stdout from the process are all read-only.
I seem to get a deadlock with whatever I try. Currently I'm trying with a thread but it still gets stuck at reading from the socket.
#!/usr/bin/haxe --interp
import sys.net.Host;
import sys.net.Socket;
import sys.io.Process;
import sys.thread.Thread;
class HaxeServer {
static function main() {
var socket = new Socket();
var fname = 'handle.sh';
var host = '127.0.0.1';
var port = 8080;
socket.bind(new Host(host), port);
socket.listen(5);
while (true) {
var conn = socket.accept();
var proc = new Process('bash', [fname]);
exchange(conn, proc);
conn.output.write(proc.stdout.readAll());
proc.close();
conn.shutdown(true, true);
conn.close();
}
}
static function exchange(conn:Socket, proc:Process):Void {
#if (target.threaded)
Thread.create(() -> {
while (true) {
var drip = conn.input.readByte();
proc.stdin.writeByte(drip);
}
});
#end
}
}
Edit 1
Attempting to use the answer posted by #YellowAfterlife, I ran the following code instead of my exchange function.
conn.setBlocking(false);
Thread.create( () -> {
trace('--> read');
while (true) {
trace('-1a');
var data:Bytes = readAllNonBlocking(conn.input).bytes;
trace('-2a');
proc.stdin.write(data);
trace('-3a');
proc.stdin.flush();
}
});
trace('--> write');
while (true) {
trace('-1b');
var data:Bytes = readAllNonBlocking(proc.stdout).bytes;
trace('-2b');
conn.output.write(data);
trace('-3b');
conn.output.flush();
}
trace('Wait');
trace(proc.exitCode());
but it just logs this and hangs:
HaxeServer.hx:42: --> write
HaxeServer.hx:44: -1b
So it's not even getting into the thread and the input is still blocking.
If I put both read-write sections in threads, it just prints 'Wait'.
I have previously dealt (on a project bridging unrelated network APIs - GitHub repo) with the issue of reading all available data without deadlocking by marking the socket as non-blocking and implementing a custom function that reads all available data, like so:
public static function readAllNonBlocking(input:Input):{bytes:Bytes,eof:Bool} {
var total:BytesBuffer = new BytesBuffer();
var eof = false;
var len = 0;
try {
while (true) {
total.addByte(input.readByte());
len += 1;
}
} catch (x:Error) {
switch (x) {
case Blocked: // OK!
default: throw x;
}
} catch (x:Eof) {
eof = true;
}
var bytes:Bytes = total.getBytes();
if (bytes.length > len) {
bytes = bytes.sub(0, len);
}
return { bytes: bytes, eof: eof };
}
You will also likely need to .flush() stdin for data to actually make it to the process.

Message lost in paho-mqtt

I am using paho-mqtt to receive data from emqx broker but I am losing data because sometimes my paho-mqtt subscriber goes down. Any method to get data when I will run subscriber again it will give all published data.
If there is no subscriber, EMQ x will discard the message, which is a normal design.
You can try EMQ x enterprise and use backend for offline data storage.
https://docs.emqx.io/tutorial/latest/en/backend/whats_backend.html
I hope you get a solution.
I don't know your client , but paho-mqtt java client can reconnect automatically. This reconnect can be check in "connectComplete(boolean reconnect, String s)" boolean parameter
public class MqttautoClient implements MqttCallbackExtended {
.
.
.
#Override
public void connectComplete(boolean reconnect, String s) {
Log.d(TAG, "Connection connectComplete");
}
#Override
public void connectionLost(Throwable throwable) {
Log.d(TAG, "Connection lost");
}
#Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception
{
Log.d(TAG, "messageArrived");
}
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
Log.d(TAG, "deliveryComplete ");
}
}

Python urllib2 fails to open web page

I have a problem where a page that I can open in Firefox, Chrome, or even Java fails to open in Python using urllib2:
import urllib2
sock = urllib2.urlopen('http://www.example.com')
li = sock.read()
sock.close()
print li
This code fails (for the particular company web page I am trying to load). The page is actually an interface to a complex back-end server, and the response we are getting is just a couple lines of (incorrect) text. At first we thought there was some bot filtering going on, but we did get the page to load using Java:
package com.ebay.marketing.rtm.components.impl.selector;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class RtmApiCheck {
private static Client mClient;
private void initClient() {
Client client = mClient;
if (client == null) {
synchronized (this) {
client = mClient;
if (client == null) {
mClient = client = Client.create();
}
}
}
}
public static void main(String[] args) {
RtmApiCheck check = new RtmApiCheck();
try {
check.initClient();
for(int i=0;i<100;i++) {
WebResource wr = mClient.resource("http://www.example.com");
ClientResponse result = wr.get(ClientResponse.class);
String strResult = result.getEntity(String.class);
System.out.println(strResult);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
What is going on with Python that causes this code to fail? Is there an alternative way to load the page that might work?

Multi-threaded task queue with shutdown in Twisted

I'm trying to get into writing multi-threaded scripts in Python for processing large lists of files. In the past, I've used utilities like GNU Parallel, but my needs are starting to exceed what that program can provide.
I've written a simple demo in Java which demonstrates what I'd like to do here:
public class ExecutorTest {
private ExecutorService executorService = Executors.newFixedThreadPool(8);
public static void main(String[] args) {
ExecutorTest t = new ExecutorTest();
t.start();
}
public ExecutorTest() {
}
public void start() {
File f = new File(".");
for (File file : f.listFiles()) {
final String fullPath = file.getAbsolutePath();
this.executorService.submit(new Runnable() {
#Override
public void run() {
// sleep for up to 100 milliseconds
try { Thread.sleep(new Double(Math.random() * 100.0).longValue()); } catch (InterruptedException e) {}
System.out.println(fullPath);
}
});
}
this.executorService.shutdown();
try {
this.executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
I'd like to do the same thing in Twisted but I'm having a difficult time understanding how all the working pieces come together so I can start a bounded multi-threaded worker queue, then wait for it to finish.
How do you write something like this in Twisted? I've done a lot of work in Python so I'm not learning Python, I'm just trying to figure out how to use Twisted.

Connect a Metro app to a Python SSL server with a self-signed certificate

How can I use a StreamSocket in a Windows 8 Metro app to connect to a Python+OpenSSL-based server using a self-signed certificate in the server ? I've tried adding the server's public key to the various trusted stores on the Windows Desktop to no avail. Everything I've tried yields the same results: an exception with the same message as in this post.
I'm developing with Visual Studio 2013 on Windows 8.1, connecting to a Python 3.4 server running OpenSSL 1.0.1h
I was able to find the answer. Microsoft provides a sample here. Once you download the sample, check out scenario 5. Based on the code in that scenario, here's a Windows 8 App Store unit test class that provides an example of how to use the code:
namespace Services.Implementation.Tests
{
using System;
using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Windows.Security.Cryptography.Certificates;
using System.Collections.Generic;
[TestClass]
public class NetworkConnectionIPv6Tests
{
#region Test Lifecycle Members
/// <summary>
/// Gets or sets the test context.
/// </summary>
/// <value>
/// The test context.
/// </value>
public TestContext TestContext
{
get; set;
}
#endregion // Test Lifecycle Members
#region Test Methods
[TestMethod, TestCategory("Integration")]
public void TestConnection()
{
const int ServerPort = 63253;
const string ServerIpAddress = "fe80::7ed1:c3ff:fed9:6fc7";
HostName hostName = new HostName(ServerIpAddress);
byte[] receiveBuffer = new byte[4096];
using (StreamSocket streamSocket = new StreamSocket())
{
bool retry = true;
do
{
try
{
streamSocket.ConnectAsync(hostName, ServerPort.ToString(), SocketProtectionLevel.Tls12).GetAwaiter().GetResult();
string certInformation = GetCertificateInformation(
streamSocket.Information.ServerCertificate,
streamSocket.Information.ServerIntermediateCertificates);
Debug.WriteLine("Certificate information: {0}", certInformation);
retry = false;
}
catch (Exception exception)
{
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
// If the exception was caused by an SSL error that is ignorable we are going to prompt the user
// with an enumeration of the errors and ask for permission to ignore.
if (streamSocket.Information.ServerCertificateErrorSeverity != SocketSslErrorSeverity.Ignorable)
{
Debug.WriteLine("Connect failed with error: " + exception.Message);
Assert.Fail("Failed to avoid unignorable errors");
}
// ---------------------------------------------------------------------------
// WARNING: Only test applications may ignore SSL errors.
// In real applications, ignoring server certificate errors can lead to MITM
// attacks (while the connection is secure, the server is not authenticated).
// ---------------------------------------------------------------------------
streamSocket.Control.IgnorableServerCertificateErrors.Clear();
foreach (var ignorableError in streamSocket.Information.ServerCertificateErrors)
{
streamSocket.Control.IgnorableServerCertificateErrors.Add(ignorableError);
}
}
} while (retry);
byte[] messageBytes = Encoding.UTF8.GetBytes("Test");
Stopwatch stopwatch = Stopwatch.StartNew();
uint bytesSent = streamSocket.OutputStream.WriteAsync(messageBytes.AsBuffer()).GetAwaiter().GetResult();
Assert.AreEqual(messageBytes.Length, (int) bytesSent, "Failed to sent the correct amount of bytes");
IBuffer bytesReceived = streamSocket.InputStream.ReadAsync(receiveBuffer.AsBuffer(), (uint) receiveBuffer.Length, InputStreamOptions.None).GetAwaiter().GetResult();
stopwatch.Stop();
Debug.WriteLine("Remote call turnaround in {0} seconds", stopwatch.Elapsed.TotalSeconds);
Assert.IsTrue(bytesReceived.Length > 0, "There were no bytes received from the server");
string responseString = new string(Encoding.UTF8.GetChars(receiveBuffer, 0, (int) bytesReceived.Length));
Assert.AreEqual("Test right back", responseString, "Failed to receive the expected message from the server");
}
}
#endregion // Test Methods
#region Helper Methods
/// <summary>
/// Gets detailed certificate information
/// </summary>
/// <param name="serverCert">The server certificate</param>
/// <param name="intermediateCertificates">The server certificate chain</param>
/// <returns>A string containing certificate details</returns>
private string GetCertificateInformation(
Certificate serverCert,
IReadOnlyList<Certificate> intermediateCertificates)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("\tFriendly Name: " + serverCert.FriendlyName);
sb.AppendLine("\tSubject: " + serverCert.Subject);
sb.AppendLine("\tIssuer: " + serverCert.Issuer);
sb.AppendLine("\tValidity: " + serverCert.ValidFrom + " - " + serverCert.ValidTo);
// Enumerate the entire certificate chain.
if (intermediateCertificates.Count > 0)
{
sb.AppendLine("\tCertificate chain: ");
foreach (var cert in intermediateCertificates)
{
sb.AppendLine("\t\tIntermediate Certificate Subject: " + cert.Subject);
}
}
else
{
sb.AppendLine("\tNo certificates within the intermediate chain.");
}
return sb.ToString();
}
#endregion // Helper Methods
}
}
The key to using this example is that you have to be using Visual Studio 2013 targeting for Windows 8.1.

Categories