0

I have code written in Java, as I don't know java wanted to establish a socket connection using python but I'm unable to understand what is python equivalent of Java getInputStream() and getOutputStream() ? And how can we use it in python ? Following, you can find the code I already wrote in python.

  import socket
    try:
        resMsg = None
        myClient = None
        myClient = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        ip = socket.gethostbyname("usddcwvtrkjxts3")
        #print(ip)
        port = 3450

        address = (ip, port)
        myClient.connect(address)
        data = myClient.recv(1024)
        print(data)
    except:
        print("connection failed")

I have following code already written in Java:

        public String invoke(String requestMsg) {
            String resMsg = null;
            Socket myClient = null;
            DataInputStream input = null;
            PrintWriter output = null;
            try {
                try {
                    myClient = new Socket("vwddtrkjxts002", 3450);
                    input = new DataInputStream(myClient.getInputStream());
                    output = new java.io.PrintWriter(myClient.getOutputStream(), true);
                    output.println(requestMsg);
                    output.write("\u001A");
                    output.flush();
                    StringBuffer message = new StringBuffer();
                    try {
                        while (true) {
                            message.append((char) input.readByte());
                        }
                    } catch (EOFException e1) {
                    }

                    resMsg = message.toString();
                    System.out.println(resMsg);
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    output.close();
                    input.close();
                    myClient.close();

                } catch (IOException e) {
                    System.out.println(e);
                }
            }
            return resMsg;
        }

I'm able to establish a connection with python but unable to print anything as output.

Any suggestion will help ! Thank you in advance

1

2 Answers 2

1

I think listen() function is what you need.

Sample code:

import socket

HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT = 65432        # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()

This is how python socket work:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

You can check a good tutorial here

In summary,

  • as client you initiate the connection, once established, you can use the socket object's recv() and send() methods to receive and send information
  • as server you bind to the port and start listening. Once the accept() method returns you have object on which you can call recv and send methods similar to client

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.