22C:078 Python Frequently Asked Questions
One way to do this is to put a parameter in the thread constructor. Suppose hiway is the class defining your threads. Its definition might start:class hiway(threading.Thread):To add a parameter to the thread constructor, we need to have an __init__ method within the hiway class definition. For example:def __init__(self,param): self.threadParameter = param threading.Thread.__init__(self)When a statement such ast = hiway((5,"intersect"))is invoked, it will create a new thread with the tuple (5,'intersect') as a parameter, which will be remembered as the self.threadParameter attribute. Now, in the run(self) method, you can refer to self.threadParameter and access those parameters for the particular thread you created earlier. That way, you can customize behavior for each thread.
I usually try Control-C or Control-D to stop the program. But I sometimes find (especially when the program is using the threading module, that these keyboard interrupts aren't effective. In those cases, I open another console window and do the commandps -u hermanwhich lists all the processes I am running (change "herman" to your own userid, of course). Then under the column PID (process id) I find one of them that is running the command "python". That's the process I want to kill; suppose the number is 16782. Then I execute the command (still in the new window)kill -9 16782The "-9" is the highest priority form of kill (the others are maybe set for stun only) and it does the job.
The help() function seems to work, but it is somewhat clumsy. I found that you can directly access the html online documents by opening the following URL in the browser, at least on the departmental linux machines:file:///usr/share/doc/python-docs-2.2.2/html/index.htmlThis is out of date (version 2.2.2 rather than 2.3), but sufficient for most purposes. But even if that doesn't work for you, the equivalent documents are somewhere on python.org.
The first sock.connect() call works OK, but the second one has an error.sock.connect(("www.google.com", 80)) sock.close() url = "www.google.com" sock.connect((url, 80))
The sock object apparently cannot be so easily reused. To fix this, put the linejust before the second sock.connect() call. This line recreates a brand new, clean and never-used socket object for you. Then the code will work just fine.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Great question! The content length is the number of bytes that follows all the headers (including, I think, the empty last line). Browsers use this to know exactly how many bytes to read without getting "stuck" in a recv() operation. This can be crucial to correct browser operation, especially when a server does not close a connection (ie, does not do sock.close() at the end). Why wouldn't a server close a connection? If the browser sends a "Connection: keep-alive" as part of the headers, that is a hint to the server that it should not close the TCP connection in case the browser will be sending another request very soon -- such as a GET request for a .jpg or .gif image -- which saves having to do extra connection setup overhead between the browser and server.
In my experience, it can be as little as just theI'm guessing this is enough, provided your server immediately closes the connection after sending the web page. But if you ever write a fancy server that can do the keep-alive trick, you would also need to have theHTTP/1.0 200 OK Content-Type: text/htmlkeyword in the headers. And remember, don't forget that empty line between the headers and the actual web page!Content-length: nnnn