Popular Python recipes tagged "socket"http://code.activestate.com/recipes/langs/python/tags/socket/2017-03-05T11:00:27-08:00ActiveState Code RecipesSend messages between computers (Python) 2014-01-01T22:11:30-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578802-send-messages-between-computers/ <p style="color: grey"> Python recipe 578802 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/chat/">chat</a>, <a href="/recipes/tags/port/">port</a>, <a href="/recipes/tags/socket/">socket</a>). </p> <p>Simple scripts to chat between computers in the same network.</p> <p>Both computers must be running both of these scripts and target ip addresses must be set correctly.</p> <p>(IP address of a computer can be found using ipconfig command.)</p> Wait for network service to appear (Python) 2014-11-06T07:29:12-08:00Mohammad Taha Jahangirhttp://code.activestate.com/recipes/users/4188847/http://code.activestate.com/recipes/578955-wait-for-network-service-to-appear/ <p style="color: grey"> Python recipe 578955 by <a href="/recipes/users/4188847/">Mohammad Taha Jahangir</a> (<a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/socket/">socket</a>). </p> <p>This script allows you to wait until specified port is opened on remote server. This can be useful in automation jobs - restarting server, wake on lan etc. It can also be used for monitoring distant service/site.</p> <p>The main problem that this script solves is that you need to handle two different timeouts when opening probing socket, and it is not described in python documentation. See <a href="http://bugs.python.org/issue5293" rel="nofollow">http://bugs.python.org/issue5293</a> for more information.</p> Server supporting IPv4 and IPv6 (Python) 2017-03-05T11:00:27-08:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578504-server-supporting-ipv4-and-ipv6/ <p style="color: grey"> Python recipe 578504 by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a> (<a href="/recipes/tags/dualstack/">dualstack</a>, <a href="/recipes/tags/ipv4/">ipv4</a>, <a href="/recipes/tags/ipv6/">ipv6</a>, <a href="/recipes/tags/ipv6_v6only/">ipv6_v6only</a>, <a href="/recipes/tags/networking/">networking</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/socket/">socket</a>). Revision 13. </p> <p>Utility functions to create a single server socket which able to listen on both IPv4 and IPv6. Inspired by: <a href="http://bugs.python.org/issue17561" rel="nofollow">http://bugs.python.org/issue17561</a></p> <p>Expected usage:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; sock = create_server_sock(("", 8000)) &gt;&gt;&gt; if not has_dual_stack(sock): ... sock.close() ... sock = MultipleSocketsListener([("0.0.0.0", 8000), ("::", 8000)]) &gt;&gt;&gt; </code></pre> <p>From here on you have a socket which listens on port 8000, all interfaces, serving both IPv4 and IPv6. You can start accepting new connections as usual:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; while True: ... conn, addr = sock.accept() ... # handle new connection </code></pre> <p>Supports UNIX, Windows, non-blocking sockets and socket timeouts. Works with Python &gt;= 2.6 and 3.X.</p> Primitive Peer to Peer Chat (Python) 2013-07-07T02:09:57-07:00teddy_khttp://code.activestate.com/recipes/users/4187115/http://code.activestate.com/recipes/578591-primitive-peer-to-peer-chat/ <p style="color: grey"> Python recipe 578591 by <a href="/recipes/users/4187115/">teddy_k</a> (<a href="/recipes/tags/chat/">chat</a>, <a href="/recipes/tags/peer/">peer</a>, <a href="/recipes/tags/select/">select</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/threading/">threading</a>). </p> <p>This took me longer than it should have. I am putting it here in the hopes that this post will spare others from having to Google things for extended periods of time.</p> <p>In short, you either listen for a connection (Chat_Server), or connect to a remote IP address (Chat_Client). From there, you can send text strings back and forth. </p> <p>This is a bit rough-hewn, obviously; I apologize in advance.</p> WebSocket interface (Python) 2012-11-25T16:52:21-08:00Nick Farohttp://code.activestate.com/recipes/users/4184363/http://code.activestate.com/recipes/578348-websocket-interface/ <p style="color: grey"> Python recipe 578348 by <a href="/recipes/users/4184363/">Nick Faro</a> (<a href="/recipes/tags/javascript/">javascript</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/websocket/">websocket</a>). Revision 2. </p> <p>This tries its best to be a replacement for the regular <code>socket</code> module.</p> <p>It supports only sending and receiving but should be useful enough.</p> <p>The only real difference should be that you can't specify the number of bytes is received, instead do</p> <pre class="prettyprint"><code>for message in socket.recv(): print(message) </code></pre> <p>Revision 2: Added proper message receiving. Previously it just requested a ton of data. Now it reads 2 bytes, determines the length, then requests that much.</p> Distributed lock manager for Python (Python) 2012-07-04T21:03:32-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578194-distributed-lock-manager-for-python/ <p style="color: grey"> Python recipe 578194 by <a href="/recipes/users/4171837/">pavel</a> (<a href="/recipes/tags/distributed/">distributed</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/locks/">locks</a>, <a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/wsgi/">wsgi</a>). Revision 2. </p> <p>Distributed lock manager provides mutex(es) over network. It is used to synchronize processes running on different machines, e.g. WSGI processes in case of web applications. Lock object is compatible with threading.Lock and can be used as a context manager ("with statement"). It can be easily modified to use UNIX sockets instead of TCP/IP. Communication protocol is text based.</p> <p>First start server process:</p> <pre class="prettyprint"><code>$ chmod +x dlm.py $ ./dlm.py </code></pre> <p>Usage:</p> <pre class="prettyprint"><code>from dlm import LockClient client = LockClient('localhost', 27272, 'client_name') lock = client.mkLock('lock_name') lock.acquire() # critical section here... lock.release() # using context manager with lock: # critical section here... </code></pre> Transparent HTTP Tunnel for Python sockets (to be used by ftplib ) (Python) 2011-11-07T10:25:56-08:00Raphaël Jolivethttp://code.activestate.com/recipes/users/4135673/http://code.activestate.com/recipes/577643-transparent-http-tunnel-for-python-sockets-to-be-u/ <p style="color: grey"> Python recipe 577643 by <a href="/recipes/users/4135673/">Raphaël Jolivet</a> (<a href="/recipes/tags/ftp/">ftp</a>, <a href="/recipes/tags/ftplib/">ftplib</a>, <a href="/recipes/tags/http/">http</a>, <a href="/recipes/tags/over/">over</a>, <a href="/recipes/tags/proxy/">proxy</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/socks/">socks</a>). Revision 4. </p> <p>This script allows how to transparently install a HTTP proxy (proxy HTTP 1.1, using CONNECT command) on all outgoing sockets.</p> <p>I did that to bring TCP over HTTP to FTPlib, transparently. It should enable HTTP tunneling for all methods / modules that use the low-level socket API.</p> Socket Broadcast Help (Python) 2011-12-04T22:18:13-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577950-socket-broadcast-help/ <p style="color: grey"> Python recipe 577950 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/broadcast/">broadcast</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/udp/">udp</a>). Revision 6. </p> <p>The classes in this module are stepping stones for building discoverable services on a network. Server replies are to be handled by the importer.</p> Pickle to/from socket (Python) 2011-04-19T22:22:53-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/577667-pickle-tofrom-socket/ <p style="color: grey"> Python recipe 577667 by <a href="/recipes/users/4171837/">pavel</a> (<a href="/recipes/tags/pickle/">pickle</a>, <a href="/recipes/tags/socket/">socket</a>). Revision 2. </p> <p>It's useful for transfering objects through socket, when doing communication between processes or networking.</p> SimpleCryptSocketExt - SimpleCrypt Wrapper for Easy Socket, Client / Server Encryption (Python) 2010-05-07T15:33:06-07:00AJ. Mayorgahttp://code.activestate.com/recipes/users/4173476/http://code.activestate.com/recipes/577212-simplecryptsocketext-simplecrypt-wrapper-for-easy-/ <p style="color: grey"> Python recipe 577212 by <a href="/recipes/users/4173476/">AJ. Mayorga</a> (<a href="/recipes/tags/client_server/">client_server</a>, <a href="/recipes/tags/cryptography/">cryptography</a>, <a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/server/">server</a>, <a href="/recipes/tags/socket/">socket</a>). Revision 5. </p> <p>Lightweight drop-in encryption wrapper for various Client/Server solutions supporting protocols such as UDP, TCP, HTTP, HTTPS, FTP, RAW Sockets etc.</p> Wait for network service to appear (Python) 2009-02-20T14:04:11-08:00anatoly techtonikhttp://code.activestate.com/recipes/users/4168147/http://code.activestate.com/recipes/576655-wait-for-network-service-to-appear/ <p style="color: grey"> Python recipe 576655 by <a href="/recipes/users/4168147/">anatoly techtonik</a> (<a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/socket/">socket</a>). Revision 2. </p> <p>This script allows you to wait until specified port is opened on remote server. This can be useful in automation jobs - restarting server, wake on lan etc. It can also be used for monitoring distant service/site.</p> <p>The main problem that this script solves is that you need to handle two different timeouts when opening probing socket, and it is not described in python documentation. See <a href="http://bugs.python.org/issue5293" rel="nofollow">http://bugs.python.org/issue5293</a> for more information.</p>