Created
October 22, 2012 16:31
-
-
Save clly/3932388 to your computer and use it in GitHub Desktop.
simple TCP Python client and server
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 * | |
HOST = 'localhost' | |
PORT = 2600 | |
ADDR = (HOST,PORT) | |
BUFFER = 4096 | |
cli = socket(AF_INET,SOCK_STREAM) | |
cli.connect((ADDR)) | |
data = cli.recv(BUFFER) | |
print data | |
cli.close() |
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 * | |
HOST = '' # or 127.0.0.1 or localhost | |
PORT = 2600 | |
ADDR = (HOST,PORT) | |
BUFFER = 4096 | |
#create a socket (SRV) | |
#see python docs for socket for more info | |
srv = socket(AF_INET,SOCKSTREAM) | |
#bind socket to address | |
srv.bind((ADDR)) #double parens create a tuple with one object | |
srv.listen(5) # maximum queued connections is 5 | |
conn,addr = srv.accept() #accepts the connection | |
print '...connected!' | |
conn.send('TEST') | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment