Created
June 24, 2016 14:21
-
-
Save dvtate/e1ff3ceda8038f61788dd373ad9ead9f to your computer and use it in GitHub Desktop.
python server test
This file contains hidden or 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 socket; | |
import sys; | |
host = "127.0.0.1"; | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM); | |
client_addr = ("127.0.0.1", 10000); | |
print ("The client is starting :D"); | |
try: | |
sock.connect(client_addr); | |
except: | |
print("ERROR: port in use/can\'t connect"); | |
new_port = raw_input("New port number: "); | |
sock.bind((host, int(new_port))); | |
#quit(); | |
try: | |
data = "hello there :P"; | |
sock.sendall(data); | |
data = sock.recv(1024); | |
print (data); | |
data = "close"; | |
sock.sendall(data); | |
data = sock.recv(1024); | |
print (data); | |
finally: | |
sock.close(); | |
This file contains hidden or 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 socket; | |
import sys; | |
host = "127.0.0.1"; | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM); | |
server_address = (host, 10000); #localhost + port 10000 | |
print ("The server is starting :D"); | |
# start working | |
try: | |
sock.bind(server_address); | |
except: | |
print("ERROR: port in use."); | |
new_port = raw_input("New port number: "); | |
sock.bind((host, int(new_port))); | |
#quit(); | |
sock.listen(1); | |
print ("Listening..."); | |
while True: | |
connection, address = sock.accept(); | |
while True: | |
data = connection.recv(1024); | |
print("recieved :D %s" %(data)); | |
connection.sendall(data); | |
if (data == "close"): | |
break; | |
connection.close(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment