So I'm new to this python stuff - and this has me stumped
# server
import SocketServer
PORT = 8037
class myRequestHandle r(SocketServer. StreamRequestHa ndler):
def handle(self):
self.input = self.rfile.read (1024)
print self.input
self.wfile.writ e("blah")
server = SocketServer.TC PServer(("", PORT), myRequestHandle r)
print "listening on port", PORT
server.serve_fo rever()
# client
import socket
HOST = socket.gethostn ame()
PORT = 8037
s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
s.connect((HOST , PORT))
s.send('Hello, world')
# Fails
data = s.recv(1024)
s.close()
print 'Received', data
Why does s.recv() hang the client? It seems like server is not
handing back "blah", but I'm sure it is.....this should be
easy...(sigh) 4 6778
Hello, Leonard, data = s.recv(1024) s.close() print 'Received', data
Why does s.recv() hang the client? It seems like server is not handing back "blah", but I'm sure it is.....
I think that the socket buffer is not full (since you are waiting for
1024 and "blah" is not enough).
this should be easy...(sigh)
If it was that easy it won't be interesting ;-)
Have a look at http://www.amk.ca/python/howto/sockets/
HTH.
Miki
"lebo" <le***********@ yahoo.com> wrote in message
news:63******** *************** ***@posting.goo gle.com... So I'm new to this python stuff - and this has me stumped
[sample code] Why does s.recv() hang the client? It seems like server is not handing back "blah", but I'm sure it is.....this should be easy...(sigh)
<gratuitous self-advertisement>
If you're going to OSCON you might like to sign up for the Python Network
Programming tutorial - see http://conferences.oreillynet.com/cs...ew/e_sess/4165
This tutorial is intended to help network programming beginners to write
their own networking code.
</gratuitous self-advertisement>
regards
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/pwp/
Miki Tebeka wrote: Hello, Leonard,
data = s.recv(1024) s.close() print 'Received', data
Why does s.recv() hang the client? It seems like server is not handing back "blah", but I'm sure it is.....
I think that the socket buffer is not full (since you are waiting for 1024 and "blah" is not enough).
This is not how the size parameter to recv() is used. It is a
*maximum*, meaning any amount of data from zero bytes (if the
socket is closed) to that value will be returned, with *no*
guarantees how much is actually returned. Often, but absolutely
not always, you will get a whole "line" and newbies will be
deceived into thinking it's enough just to recv(1024) and
carry on, but eventually such code will fail.
-Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Olivier Hoarau |
last post by:
Hello,
I have build a client/server application with the socket module. The
server mades UDP broadcasting and the client only reads UDP broadcast
messages. All work fine.
Now I want to use for the same thing the socketserver module, it's ok
for the client, but I don't succeed in making work the server :-((
Here is the client (which works)
|
by: Ergin Aytac |
last post by:
I'm trying to run a script written in python and have some socket
connection problems. I cutted the origin script (more than 1000 lines)
so it is only the part of the connection and there is no functionalty
but connecting to a server.
I have no idea about programming with python so every hint is a present
for me :)
------------------------------------
import SocketServer
import os
|
by: Adil Hasan |
last post by:
Hello Fred,
I just ran across your question. I think that the following
code will work:
----- SERVER CODE ------
import SocketServer
import time
class GreetingHandler(SocketServer.BaseRequestHandler):
'''Class to handle sending a greeting to a client
'''
|
by: missiplicity |
last post by:
Hi,
I am newbie to Python language and am taking my baby steps. I am
using Python2.4 from ActiveState on W2K. I am trying to create a simple
SocketServer program. Just adding the following 2 lines of code in the
program result in the errors given below:
import SocketServer
print "SocketServer imported"
|
by: Paul Rubin |
last post by:
Let's say you have a SocketServer with the threading mix-in and you
run serve_forever on it. How can you shut it down, or rather, how can
it even shut itself down? Even if you use a handle_request loop
instead of serve_forever, it still seems difficult:
class myserver(ThreadingMixIn, TCPServer): pass
server = myserver(...)
server.shutdown = False
while not server.shutdown:
server.handle_request()
| |
by: Magnus Lycka |
last post by:
I have a socket server like below which I want to exit when
it's out of data. If I interrupt the client, I'll get a
broken pipe on the server side, and after a Ctrl-C, I can
restart the server again, but if I let it run out of data,
and exit via handle_error as can be seen below, I will get
a socket.error: (98, 'Address already in use') when I try
to restart the server. (Unless I wait a minute or so, or
use another port.) I feel like I'm...
|
by: Tomi Hautakoski |
last post by:
Hello,
I'm a Python newbie trying to figure out how to use SocketServer with
IPv6. I would like to set up a TCPServer working like below but how to
tell SocketServer I need to use AF_INET6?
import SocketServer
import logging as l
l.basicConfig(level=l.DEBUG,
|
by: eliben |
last post by:
Hello,
I have a small wxPython application. Today I was trying to add some
RPC capability to it, so I implemented an instance of
SimpleXMLRPCServer that runs in a separate thread when invoked and
answers requests.
All went fine until I realized that I have to sometimes stop the
server - which is where I ran into a problem. Python threads can not
be killed after they've been started. They can be kindly requested to
|
by: id.engg |
last post by:
logFileName = 'log.txt'
logfile = open(logFileName, "a")
class MyUDPServer(SocketServer.UDPServer):
def server_bind(self):
self.socket.setsockopt(socket.SOL_SOCKET,
socket.SO_RCVBUF, 8388608)
self.socket.bind(self.server_address)
class LogsDumpHandler(SocketServer.DatagramRequestHandler):
def handle(self):
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |