Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

[python][network]   simplest networking

Posted on 2007-11-23
2
Medium Priority
?
328 Views
Last Modified: 2010-04-16
I just want python to send a line of text to the other script on the other computer


they are on the same network,

so,

just a server script listening for

a client script sending one line of text,


is it possible?
0
Comment
Question by:Troudeloup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Expert Comment

by:ramrom
ID: 20341256
Yes - see the socket module
0
 
LVL 29

Accepted Solution

by:
pepr earned 2000 total points
ID: 20344140
If you are allowed, see the question http:#21995292 and the answers. If you cannot search for the answers, here are copies of my answers...

1) Focus on the built-in module socket (Python doc "7.2 socket -- Low-level networking interface"). See the examples:

http://www.qiksearch.com/python/sockets/server.py
http://www.qiksearch.com/python/sockets/client.py

They are a bit old and could be written aestetically better, but they show the principle. (Add the line UDPSock.sendto('', addr) before the break to finish the server when sending empty line.)

Later, you may get info and inspiration in the standard module SocketServer Python doc ("11.16 SocketServer -- A framework for network servers")

Use the snippets below, store them in separate files -- one for the server, one for the client. The UDP is the simplest form of usage of the IP protocol. See http://en.wikipedia.org/wiki/User_Datagram_Protocol. More reliable transfer can be done via TCP (see http://en.wikipedia.org/wiki/Transmission_Control_Protocol).
# Server program
 
import socket
 
# Set the socket parameters
addr = ('', 33333)  # host, port
 
# Create socket and bind to address
UDPSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
UDPSock.bind(addr)
 
# Receive messages
while True:
    data, addr = UDPSock.recvfrom(1024)
    if data == 'stop':
        print 'Client wants me to stop.'
        break
    else:
        print "From addr: '%s', msg: '%s'" % (addr[0], data)
 
# Close socket
UDPSock.close()
print 'Server stopped.'
 
---------------------------------------------------------------
 
# Client program
 
import socket
 
 
#addr = ('localhost', 33333)                                # localhost, port
#addr = ('127.0.0.1', 33333)                                # localhost explicitly
#addr = ('xyz', 33333)                                      # explicit computer
#addr = ('<broadcast>', 33333)                              # broadcast address
addr = ('255.255.255.255', 33333)                          # broadcast address explicitly
 
UDPSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Create socket
 
print 'Enter your message:'
print '- Empty message to stop this client.'
print '- "stop" to stop all servers.'
 
# Almost infinite loop... ;)
while True:
    data = raw_input('>> ')
    if len(data) == 0:
        break
    else:
        if UDPSock.sendto(data, addr):
            print "Sending message '%s'..." % data
 
UDPSock.close()             # Close socket
print 'Client stopped.'

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question