This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Memo for network programming by python | |
''' | |
import socket | |
host = "foo.bar.com" | |
port = 12345 | |
msg = "Hello" | |
buffersize = 4096 | |
## Create TCP socket | |
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
## Connect to the host | |
client.connect((host, port)) | |
## Send some data | |
client.send(msg) | |
## Recieve some data | |
response = client.recv(buffersize) | |
## Create UDP socket | |
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
## UDP is a connectionless protocol so no need to call connect() beforehand | |
## Send some data | |
client.sendto(msg, (host, port)) | |
## Recieve some data | |
recvdata, addr = client.recvfrom(buffersize) #recvfrom returns both data and the detail of the remote host and port, AKA address |