Python and Java Socket Communication instance code, pythonsocket
Socket Communication Between Python and Java
I have previously worked on a Java communication tool with basic functions such as sending a message and sending a file. but we all know that the interfaces written in Java, whether AWT or Swing, are simply not viewed by people. It is good for our developers. If it is Release, we will show it to users, it must be despised to the end. if C ++ is used, a lot of code is written (QT is doing well in this aspect !), However, I use Python here so that wxPython can be used as the interface, and the two platforms are also doing very well.
Here we only provide the core implementation and ideas
Server (Java) receives files sent from Clinet (Python)
JServer. java
Import java. io. bufferedReader; import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java.net. serverSocket; import java.net. socket; public class JServer implements Runnable {ServerSocket ss; public JServer () throws Exception {ss = new ServerSocket (8086); new Thread (this ). start () ;}@ Override public void run () {Int I = 0; System. out. println ("server startup. "); while (true) {try {Socket s = ss. accept (); // each client has a processing thread new Handler (s, I ). start (); I ++;} catch (IOException e) {e. printStackTrace () ;}} public static void main (String [] args) {try {new JServer ();} catch (Exception e) {e. printStackTrace () ;}} class Handler extends Thread {Socket s; int id; public Handler (Socket s, int id) {this. S = s; this. id = id ;}@ Override public void run () {System. out. println ("in handling .. "); FileOutputStream fos = null; try {InputStream is = s. getInputStream (); BufferedReader in = new BufferedReader (new InputStreamReader (is); // read the file name String filename = in from the client. readLine (); System. out. println ("read line" + id + ":" + filename); File file = new File (filename); int len = 0; int BUFSIZE = 4*1 024; byte [] by = new byte [BUFSIZE * 1024]; fos = new FileOutputStream (file); while (len = is. read (by, 0, BUFSIZE ))! =-1) {fos. write (by, 0, len); fos. flush ();} System. out. println ("done. ");} catch (Exception e) {e. printStackTrace ();} finally {// The server end should not shut down the socket; otherwise, the Python will have an error Errno 10054, so that the client can be turned off. try {fos. close ();} catch (IOException e) {e. printStackTrace ();}}}}
Python Client
#-*-Coding: UTF-8 -*-#! /Usr/bin/python # coding = utf-8import timeimport threadingimport socketimport OS class Client (): def _ init _ (self): address = ('2017. 0.0.1 ', 8086) s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) s. connect (address) fn = 'test.zip 'ff = OS. path. normcase (fn) try: f = open (fn, 'rb') sendFile = SendFile (s, f) sendFile. start () print 'start to send file. 'handle t IOError: print 'open err' class SendFile (Threading. thread): def _ init _ (self, sock, file): threading. thread. _ init _ (self) self. file = file self. sock = sock def run (self): print self. file BUFSIZE = 1024 count = 0 name = self. file. name + '\ R' # The first 1 K bytes must be added to the file name to be sent to the server. Otherwise, the server will not be able to read the line for I in range (1, BUFSIZE-len (self. filename)-1): name + = '? 'Print name self. sock. send (name) while True: print BUFSIZE fdata = self. file. read (BUFSIZE) if not fdata: print 'no data. 'break self. sock. send (fdata) count + = 1 if len (fdata )! = BUFSIZE: print 'count: '+ str (count) print len (fdata) nRead = len (fdata) print 'send file finished. 'self. file. close () self. sock. close () print 'close socket 'C = Client ()
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!