Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Tutorials
Python sockets
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="griimnak" data-source="post: 365730" data-attributes="member: 35695"><p><span style="font-size: 26px">What is a socket?</span></p><p>A <strong>socket</strong> is one endpoint of a two-way communication link between two programs running on the network. A <strong>socket</strong> is bound to a port number so that the <strong>TCP</strong> layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.</p><p></p><p>If you're someone that mods habbo, you use sockets quite frequently when running an emulator with a dedicated game port and MUS port. For example, a habbo client will connect to your emulator which is streaming on socket 30000.</p><p></p><p><span style="font-size: 26px">Creating and interacting with sockets via Python</span></p><p>Right off the bat, you'll want to import python's built in socket module.</p><p>[CODE=PHP]import socket[/CODE]</p><p></p><p><span style="font-size: 12px">This is an example socket server to listen for incoming data:</span></p><p>[CODE=PHP]</p><p>import socket</p><p>import threading</p><p></p><p>class SocketServer(object):</p><p> def start(self):</p><p> # Variables</p><p> bind_ip = '0.0.0.0'</p><p> bind_port = '999999'</p><p> # Create server instance</p><p> server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</p><p> server.bind((bind_ip,bind_port)) # bind</p><p> server.listen(5) # listen</p><p> print "[*] Listening on %s:%d" % (bind_ip,bind_port)</p><p></p><p> def handleConnection(client_socket):</p><p> request = client_socket.recv(1024)</p><p> if request == 'poop':</p><p> print 'HAX! ULTIMATE HAX!'</p><p></p><p> print "[+] Received: %s" % request</p><p> # if there's a request to connect, do this;</p><p> while True:</p><p> client,addr = server.accept()</p><p> print "[*] Accepting connection: %s:%d" % (addr[0],addr[1])</p><p></p><p> client_handler = threading.Thread(target=handleConnection,args=(client,))</p><p> client_handler.start()</p><p></p><p></p><p>[/CODE]</p><p></p><p></p><p><span style="font-size: 12px">Now you can use this client to send data to the listening server:</span></p><p>[CODE=PHP]</p><p>import socket</p><p></p><p>class Messager(object):</p><p> def requestInput(self):</p><p> target_ip = ''</p><p> target_port = ''</p><p></p><p></p><p> print '[*] Client now bound to %s:%d' % (target_ip, target_port)</p><p></p><p> while True:</p><p> inputfield = raw_input("[*] >> ")</p><p></p><p> if inputfield == '/ping':</p><p> </p><p> print 'pingping!'</p><p></p><p> elif inputfield != '':</p><p> # client instance</p><p> client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</p><p> client.connect((target_ip, target_port))</p><p> client.send(inputfield)</p><p></p><p></p><p>[/CODE]</p><p></p><p><span style="font-size: 26px">Explanation </span></p><p>On the server, you should configure<strong> bind_ip</strong> and <strong>bind_port</strong> to the desired port and ip you wish to bind the server to. If you wish to run the server local, the ip should either be 127.0.0.1 or 0.0.0.0.</p><p>On the client, you should configure <strong>target_ip</strong> and <strong>target_port</strong> to match the details of your server. With that done, your server will listen on your ip<img src="/styles/default/xenforo/smilies/emojione/tongue.png" class="smilie" loading="lazy" alt=":p" title="Stick Out Tongue :p" data-shortname=":p" />ort. You can then you use your client to send text data via TCP sockets, with python!</p><p></p><p>With alot of logic, you can do anything with sockets. From game development to remote administration tools, anything is possible really.</p><p><strong>I included some dummy logic in the server. If it receives "poop" it will spit some shit out.</strong></p><p><strong></strong></p><p>Your output should look something like this (minus the pretty stuff):</p><p><img src="http://i.imgur.com/KdUYDOJ.gif" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p><span style="font-size: 26px">Python Sockets Documentation</span></p><p>Official python docs: <a href="https://docs.python.org/2/library/socket.html" target="_blank">here</a> & <a href="https://docs.python.org/2/howto/sockets.html" target="_blank">here</a></p><p>IBM developers docs: <a href="https://www.ibm.com/developerworks/linux/tutorials/l-pysocks/" target="_blank">here</a></p><p></p><p><em>AND you can download the source to the aegis test server above if you want; <a href="http://www.mediafire.com/download/7139l3nz68cdvs2/Aegis.zip" target="_blank">here</a></em></p><p>cheers</p></blockquote><p></p>
[QUOTE="griimnak, post: 365730, member: 35695"] [SIZE=7]What is a socket?[/SIZE] A [B]socket[/B] is one endpoint of a two-way communication link between two programs running on the network. A [B]socket[/B] is bound to a port number so that the [B]TCP[/B] layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number. If you're someone that mods habbo, you use sockets quite frequently when running an emulator with a dedicated game port and MUS port. For example, a habbo client will connect to your emulator which is streaming on socket 30000. [SIZE=7]Creating and interacting with sockets via Python[/SIZE] Right off the bat, you'll want to import python's built in socket module. [CODE=PHP]import socket[/CODE] [SIZE=3]This is an example socket server to listen for incoming data:[/SIZE] [CODE=PHP] import socket import threading class SocketServer(object): def start(self): # Variables bind_ip = '0.0.0.0' bind_port = '999999' # Create server instance server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((bind_ip,bind_port)) # bind server.listen(5) # listen print "[*] Listening on %s:%d" % (bind_ip,bind_port) def handleConnection(client_socket): request = client_socket.recv(1024) if request == 'poop': print 'HAX! ULTIMATE HAX!' print "[+] Received: %s" % request # if there's a request to connect, do this; while True: client,addr = server.accept() print "[*] Accepting connection: %s:%d" % (addr[0],addr[1]) client_handler = threading.Thread(target=handleConnection,args=(client,)) client_handler.start() [/CODE] [SIZE=3]Now you can use this client to send data to the listening server:[/SIZE] [CODE=PHP] import socket class Messager(object): def requestInput(self): target_ip = '' target_port = '' print '[*] Client now bound to %s:%d' % (target_ip, target_port) while True: inputfield = raw_input("[*] >> ") if inputfield == '/ping': print 'pingping!' elif inputfield != '': # client instance client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((target_ip, target_port)) client.send(inputfield) [/CODE] [SIZE=7]Explanation [/SIZE] On the server, you should configure[B] bind_ip[/B] and [B]bind_port[/B] to the desired port and ip you wish to bind the server to. If you wish to run the server local, the ip should either be 127.0.0.1 or 0.0.0.0. On the client, you should configure [B]target_ip[/B] and [B]target_port[/B] to match the details of your server. With that done, your server will listen on your ip:port. You can then you use your client to send text data via TCP sockets, with python! With alot of logic, you can do anything with sockets. From game development to remote administration tools, anything is possible really. [B]I included some dummy logic in the server. If it receives "poop" it will spit some shit out. [/B] Your output should look something like this (minus the pretty stuff): [IMG]http://i.imgur.com/KdUYDOJ.gif[/IMG] [SIZE=7]Python Sockets Documentation[/SIZE] Official python docs: [URL='https://docs.python.org/2/library/socket.html']here[/URL] & [URL='https://docs.python.org/2/howto/sockets.html']here[/URL] IBM developers docs: [URL='https://www.ibm.com/developerworks/linux/tutorials/l-pysocks/']here[/URL] [I]AND you can download the source to the aegis test server above if you want; [URL='http://www.mediafire.com/download/7139l3nz68cdvs2/Aegis.zip']here[/URL][/I] cheers [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Tutorials
Python sockets
Top