Skip to content

Instantly share code, notes, and snippets.

@Tony3-sec
Last active September 17, 2017 16:35
Show Gist options
  • Save Tony3-sec/7f275ac1b6dc2bf38b9764aadf62e8b5 to your computer and use it in GitHub Desktop.
Save Tony3-sec/7f275ac1b6dc2bf38b9764aadf62e8b5 to your computer and use it in GitHub Desktop.
'''
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment