Last active
August 29, 2015 14:17
-
-
Save dufferzafar/b77d80d85708063f5036 to your computer and use it in GitHub Desktop.
Python Socket Program.
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
import sys | |
from socket import socket, AF_INET, SOCK_DGRAM | |
SERVER_IP = '127.0.0.1' | |
PORT_NUMBER = 5000 | |
SIZE = 1024 | |
print("Test client sending packets to IP {0}, via port {1}\n".format(SERVER_IP, PORT_NUMBER)) | |
mySocket = socket(AF_INET, SOCK_DGRAM) | |
while True: | |
# Get Input & Send to Server | |
text = raw_input("Enter some text:") | |
mySocket.sendto(text, (SERVER_IP, PORT_NUMBER)) | |
# Recieve Reply & Print | |
(data, addr) = mySocket.recvfrom(SIZE) | |
print data | |
sys.exit() |
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
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM | |
import sys | |
PORT_NUMBER = 5000 | |
SIZE = 1024 | |
hostName = gethostbyname('0.0.0.0') | |
mySocket = socket(AF_INET, SOCK_DGRAM) | |
mySocket.bind((hostName, PORT_NUMBER)) | |
print("Test server listening on port {0}\n".format(PORT_NUMBER)) | |
while True: | |
# Recieve data from client | |
(data, addr) = mySocket.recvfrom(SIZE) | |
print data | |
# Calculate and send | |
ones = data.count("1") | |
zeroes = data.count("0") | |
mySocket.sendto("Number of 1s is %d, while 0s is %d" % (ones, zeroes), addr) | |
sys.ext() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nifty!