Polyglot CheatSheet - Network
HTTP Server
Java
Introduced in Java 18 (JEP 408: Simple Web Server).
$ jwebserver
# Specify port 9000
$ jwebserver -p 9000
Python
Start HTTP server
Python 3.x:
$ python -m http.server
Serving HTTP on 0.0.0.0 port 8000 ...
Python 2.x:
$ python -mSimpleHTTPServer 8080
Read Web Pages
Java
final URL url = new URL("http://ichart.finance.yahoo.com/table.csv?s=" + ticker);
final BufferedReader reader = new BufferedReader( new InputStreamReader(url.openStream()));
Python
Download pages from wikipedia
import urllib.request
opener = urllib.request.build_opener()
opener.addheaders =[('User-agent','Mozilla/5.0')]
infile = opener.open('http://en.wikipedia.org/w/index.php?title=Albert_Einstein&printable=yes')
page = infile.read()
Network
Java
In java.net
, Java programs can use TCP or UDP to communicate over the Internet. The URL
, URLConnection
, Socket
, and ServerSocket
classes all use TCP to communicate over the network. The DatagramPacket
, DatagramSocket
, and MulticastSocket
classes are for use with UDP.
java.net.URL vs java.net.URI: java.net.URL#equals
is blocking:
Two hosts are considered equivalent if both host names can be resolved into the same IP addresses; else if either host name can't be resolved, the host names must be equal without regard to case; or both host names equal to null. Since hosts comparison requires name resolution, this operation is a blocking operation.