Created
May 13, 2016 15:16
-
-
Save hackerdem/7863dcfea0d8654735cbfe83c17a872e to your computer and use it in GitHub Desktop.
tcp client application in python
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 | |
import argparse | |
host='localhost' | |
def echo_client(port): | |
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
server_address=(host,port) | |
print("connection to {} port {}".format(host,port)) | |
sock.connect(server_address) | |
try: | |
message="hey mate what's up" | |
print("sending {}".format(message)) | |
data=message.encode('ascii') | |
sock.sendall(data) | |
amount_received=0 | |
amount_expected=len(message) | |
while amount_received<amount_expected: | |
data=sock.recv(16) | |
amount_received+=len(data) | |
print("received {}".format(data)) | |
except socket.error as e: | |
print ("socket error {}".format(str(data))) | |
except Exception as e: | |
print ("Other exception {}".format(str(e))) | |
finally: | |
print("connection terminated") | |
sock.close() | |
def main(): | |
parser=argparse.ArgumentParser(description='My Socket Server') | |
parser.add_argument('--port',action="store",dest="port",type=int,required=True) | |
given_args=parser.parse_args() | |
port=given_args.port | |
echo_client(port) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is there a code to insert data from gps device into database? im using OsmAnd protocol