Ejemplo n.º 1
0
 def __init__(self, get_mask_key = None, sockopt = ()):
     """
     Initalize WebSocket object.
     """
     self.connected = False
     self.io_sock = self.sock = socket.socket()
     for opts in sockopt:
         self.sock.setsockopt(*opts)
     self.get_mask_key = get_mask_key
Ejemplo n.º 2
0
def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('', 9090))
    sock.listen(5)

    while True:
        (client, addr) = sock.accept()
        csock_fd = client.makefile()
        ping(csock_fd)
Ejemplo n.º 3
0
def retBanner(ip, port):
    try:
        socket.setdefaulttimeout(2)
        s = socket.socket()
        s.connect((ip, port))
        banner = s.recv(1024)
        return banner
    except:
        return
Ejemplo n.º 4
0
def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('localhost',9090))
    csock_fd = sock.makefile()
    while True:
        try:
            line = csock_fd.readline() 
            count = int(line.strip("\r\n"))
            print "recv : %d" % count
            
            time.sleep(1)
            csock_fd.write("%d\n" % (count+1))
            print "send : %d" % (count+1)
            csock_fd.flush()
        except IOError:
            print "error"
#
"""Client half of echo example
"""
#end_pymotw_header

import sys
from network_programming import socket

messages = [ 'This is the message. ',
             'It will be sent ',
             'in parts.',
             ]
server_address = ('localhost', 10000)

# Create a TCP/IP socket
socks = [socket.socket(socket.AF_INET, socket.SOCK_STREAM),
         socket.socket(socket.AF_INET, socket.SOCK_STREAM),
         ]

# Connect the socket to the port where the server is listening
print >>sys.stderr, 'connecting to %s port %s' % server_address
for s in socks:
    s.connect(server_address)

for message in messages:

    # Send messages on both sockets
    for s in socks:
        print >>sys.stderr, '%s: sending "%s"' % \
            (s.getsockname(), message)
        s.send(message)
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Client half of echo example
"""
#end_pymotw_header

import sys
from network_programming import socket

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

server_address = ('localhost', 10000)
message = 'This is the message.  It will be repeated.'

try:

    # Send data
    print >>sys.stderr, 'sending "%s"' % message
    sent = sock.sendto(message, server_address)

    # Receive response
    print >>sys.stderr, 'waiting to receive'
    data, server = sock.recvfrom(4096)
    print >>sys.stderr, 'received "%s"' % data

finally:
    print >>sys.stderr, 'closing socket'
    # null data means the client is finished with us
    while data:
        if not do_something(connstream, data):
            break
        data = connstream.read()

def do_something(connstream, data):
    print data
    connstream.write('pong')

if __name__ == "__main__":
    host = "0.0.0.0"
    port = 7474

    context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    mycertfile=os.path.join(os.path.dirname(__file__),'mycertfile')
    mykeyfile=os.path.join(os.path.dirname(__file__),'mykeyfile')
    context.load_cert_chain(certfile=mycertfile, keyfile=mykeyfile)
    bindsocket = socket.socket()
    bindsocket.bind((host, port))
    bindsocket.listen(5)
    while True:
        newsocket, fromaddr = bindsocket.accept()
        connstream = ssl.wrap_socket(newsocket, server_side=True, certfile=mycertfile, keyfile=mykeyfile)
        try:
            deal_with_client(connstream)
        finally:
            connstream.shutdown(socket.SHUT_RDWR)
            connstream.close()

#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Server half of echo example.
"""
#end_pymotw_header

import Queue
import select
import sys
from network_programming import socket

# Create a TCP/IP socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(0)

# Bind the socket to the port
server_address = ('localhost', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
server.bind(server_address)

# Listen for incoming connections
server.listen(5)

# Keep up with the queues of outgoing messages
message_queues = {}

# Do not block forever (milliseconds)
TIMEOUT = 1000
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Client half of echo example
"""
#end_pymotw_header

import sys
from network_programming import socket

# Create a UDS socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = './uds_socket'
print >>sys.stderr, 'connecting to %s' % server_address
try:
    sock.connect(server_address)
except socket.error, msg:
    print >>sys.stderr, msg
    sys.exit(1)

try:
    
    # Send data
    message = 'This is the message.  It will be repeated.'
    print >>sys.stderr, 'sending "%s"' % message
    sock.sendall(message)
#!/usr/bin/env python
# encoding: utf-8
from network_programming import socket

if __name__ == '__main__':
    try:
        import ssl
    except ImportError:
        pass
    host = 'localhost'
    port = 7474
    context = ssl.create_default_context()
    context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    context.verify_mode = ssl.CERT_REQUIRED
    context.load_verify_locations("mycertfile")
    conn = context.wrap_socket(socket.socket(socket.AF_INET), server_hostname=host)
    conn.connect((host, port))
    conn.sendall("ping")
    print conn.recv(1024)